如何在C#中将Datagridview导出到Excel [英] How Can I Export Datagridview To Excel In C#

查看:86
本文介绍了如何在C#中将Datagridview导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码

i use this code

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();
        }  



和按钮中的这一个




and this one in the button

private void button6_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel Documents (*.xlsx)|*.xlsx";
            sfd.FileName = "export.xlsx";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //ToCsV(dataGridView1, @"c:\export.xls");
                ToCsV(dataGridView1, sfd.FileName); // Here dataGridview1 is your grid view name
                MessageBox.Show("Excel Exported!");
            }
        }







但是当我尝试打开文件时说文件格式不正确



任何人都可以帮忙请




but when i try to open the file it say the file is not in a right format

can anyone help please

推荐答案

你有点儿在这里混淆了。首先创建一些制表符分隔的字符串,而不是CSV的最佳思路(即逗号分隔值)。然后,对数据使用一些奇怪的编码,并将其作为二进制流写入文件,但为文件名提供 .xlsx 扩展名。 CSV文件是纯文本,每个字段应以逗号分隔。文件名的扩展名应为 .csv ,以便Excel可以正确解码它。
You are getting somewhat mixed up here. You start by creating some tab separated strings, not the best idea for CSV (i.e. Comma Separated Values). You then use some strange encoding on your data and write it out to the file as a binary stream, but giving the file name a .xlsx extension. CSV files are pure text and should have each field separated by commas. The extension for the filename should be .csv so that Excel can correctly decode it.


正如理查德所说,你没有制作一个代码中的ToCSV文档。看起来您正在尝试制作一个实际的.xlsx文件。你不能这样做。如果您遵循理查德的建议,您可以创建一个在Excel中正确打开的.csv文件。这通常足以满足大多数需求。



但是如果你真的想要创建一个真正的xlsx文件,你将需要更多的工作。



这里有一个链接可以让你知道你在看什么。

如何读写.xlsx(Excel 2007)文件 - 第一部分 [ ^ ]
As Richard said, you are not making a 'ToCSV' document in your code. It looks like you are trying to make an actual .xlsx file. You can't do it that way. If you follow Richard's advice you can make a .csv file that will open correctly in excel. This is usually sufficient for most needs.

However if you really want to create a real xlsx file you will need a lot more work.

Here's a link to give you some idea of what you are looking at.
How to Read and Write .xlsx (Excel 2007) file - Part I[^]


谢谢richard和bowltuner



i会尝试做到这一点谢谢你你的建议



ps。我只是一个初学者编程sry,如果我这样做:(
thank you richard and bowltuner

i will try to do it thank you for your advise

ps. i am just a beginner programming sry if i am to stupit about this :(


这篇关于如何在C#中将Datagridview导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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