将datagridview和label vaue导出为ex​​cel [英] Exporting datagridview and label vaue to excel

查看:129
本文介绍了将datagridview和label vaue导出为ex​​cel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含帐单详细信息的datagridview。还有一个包含总金额的标签。我想在Excel中将datagridview值添加到excel和datagridview值末尾的标签总数。



这里我使用函数将datagridview导出到excel 。并在点击事件中调用它。



private void ToCsV(DataGridView dGV,string filename)

{

string stOutput =;

//导出标题:

string sHeaders =;



for(int j = 0; j< dGV.Columns.Count; j ++)

sHeaders = sHeaders.ToString()+ Convert.ToString(dGV.Columns [j] .HeaderText)+\\ \\ t;

stOutput + = sHeaders +\\\\ n;

//导出数据。

for(int i = 0; i< dGV.RowCount - 1; i ++)

{

string stLine =;

for(int j = 0; j< dGV.Rows [i] .Cells.Count; j ++)

stLine = stLine.ToString()+ Convert.ToString(dGV.Rows [i] .Cells [j] .Value)+\t;

stOutput + = stLine +\\\\ n;

}

编码utf16 = Encoding.GetEncoding(125 4);

byte [] output = utf16.GetBytes(stOutput);

FileStream fs = new FileStream(filename,FileMode.Create);

BinaryWriter bw = new BinaryWriter(fs);

bw.Write(output,0,output.Length); //编写编码文件

bw.Flush();

bw.Close();

fs.Close();

}

现在我想将标签值添加到excel数据的末尾。我看过添加标签值的示例。但我的问题是我想在excel中的数据末尾添加它。所以它可以称为总数。我怎样才能做到这一点。我坚持了下来。有人能帮助我吗?



我尝试过的事情:



我已将datagridview值导出为ex​​cel。

解决方案

您生成了一个制表符分隔值(TSV)文件,因此您几乎无法控制格式。要将总数添加到文件中,您只需要输出包含总数的另一行。



但是,您的代码当前在循环中使用字符串连接。这将是非常低效的 - 每次添加一个新字符串时,.NET必须创建一个新字符串,将旧字符串复制到开头,然后复制你追加到最后的字符串。



您可以使用 StringBuilder [ ^ ]构建字符串。但在这种情况下,由于您将输出写入文件,因此最好使用 StreamWriter [ ^ ]。

 使用 var  fs =  new  FileStream(filename,FileMode.Create))
< span class =code-keyword>使用( var writer = new StreamWriter(fs ,Encoding.GetEncoding( 1254 )))
{
// 写标题:
for int j = 0 ; j < dGV.Columns.Count; j ++)
{
if (j!= 0 )writer.Write( ' \t');
writer.Write(dGV.Columns [j] .HeaderText);
}
writer.WriteLine();

// 导出数据:
for int i = 0 ; i < dGV.RowCount - 1 ; i ++)
{
var row = dGV.Rows [i];

for int j = 0 ; j < dGV.Columns.Count; j ++)
{
if (j!= 0 )writer.Write(' \t');
writer.Write(row.Cells [j] .Value);
}

writer.WriteLine();
}

// 添加总额:
writer.Write( Total:);
writer.Write(' \t');
writer.Write(YourLabel.Text);
writer.WriteLine();
}



如果你想要更多地控制结果的格式和布局,那么你需要使用一个可以生成真实Excel文件的工具。例如:


I have a datagridview which contains bill details. And also a label which contains the total amount. I want to add the datagridview values to excel and the total from label at the end of the datagridview values in excel.

here im using a function to export datagridview to excel. and calling it in a click event.

private void ToCsV(DataGridView dGV, string filename)
{
string stOutput = "";
// Export titles:
string sHeaders = "";

for (int j = 0; j < dGV.Columns.Count; j++)
sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
stOutput += sHeaders + "\r\n";
// Export data.
for (int i = 0; i < dGV.RowCount - 1; i++)
{
string stLine = "";
for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
stOutput += stLine + "\r\n";
}
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output = utf16.GetBytes(stOutput);
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(output, 0, output.Length); //write the encoded file
bw.Flush();
bw.Close();
fs.Close();
}
Now i want to add the label value which is total to the end of excel data. I have seen examples to add label values. But my problem is i want to add it at the end of data in excel. So it can be referred as total. How can i do that. Im stuck with it. Can someone help me?

What I have tried:

I have exported datagridview value to excel.

解决方案

You're generated a Tab-Separated Values (TSV) file, so you have very little control over the formatting. To add the total to the file, you just need to output another row containing the total.

However, your code is currently using string concatenation in a loop. That's going to be horribly inefficient - each time you add a new string, .NET has to create a new string, copy the old string to the start, and then copy the string you're appending to the end.

You could use a StringBuilder[^] to build the string. But in this case, since you're writing the output to a file, it would probably be better to use a StreamWriter[^].

using (var fs = new FileStream(filename, FileMode.Create))
using (var writer = new StreamWriter(fs, Encoding.GetEncoding(1254)))
{
    // Write the headers:
    for (int j = 0; j < dGV.Columns.Count; j++)
    {
        if (j != 0) writer.Write('\t');
        writer.Write(dGV.Columns[j].HeaderText);
    }
    writer.WriteLine();
    
    // Export the data:
    for (int i = 0; i < dGV.RowCount - 1; i++)
    {
        var row = dGV.Rows[i];
        
        for (int j = 0; j < dGV.Columns.Count; j++)
        {
            if (j != 0) writer.Write('\t');
            writer.Write(row.Cells[j].Value);
        }
        
        writer.WriteLine();
    }
    
    // Add the total:
    writer.Write("Total:");
    writer.Write('\t');
    writer.Write(YourLabel.Text);
    writer.WriteLine();
}


If you want more control over the formatting and layout of the result, then you'll need to use a tool which can generate a real Excel file. For example:


这篇关于将datagridview和label vaue导出为ex​​cel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆