我想在excel文件中导出带有整个标头的datagridview值 [英] I want to export datagridview value with whole header in excel file

查看:85
本文介绍了我想在excel文件中导出带有整个标头的datagridview值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究考勤软件,我的项目差不多完成了,我在excel中导出Datagridview数据时只有一个问题,它也工作正常,但它没有保存Datagridview列的整个标题,请查看截图和请帮助我,我该怎么做呢谢谢这是我的Datagridview的完整标题 https://imgur.com/8ZdEOd0 这里是截图导出后我的excel文件 https://imgur.com/L2IypZZ



我尝试了什么:



I am working on attendance software and my project is almost done, I have just one issue while exporting Datagridview data in excel and it's also working fine just but it does not save the whole header of Datagridview columns please check out the screenshot and please help me how can I do this thanks here is my full header of Datagridview https://imgur.com/8ZdEOd0 here is the screenshot of my excel file after export https://imgur.com/L2IypZZ

What I have tried:

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

            for (int j = 0; j < dataGridView4.Columns.Count; j++)
                sHeaders = sHeaders.ToString() + Convert.ToString(dataGridView4.Columns[j].HeaderText) + "\t";
            stOutput += sHeaders + "\r\n";
            // Export data.
            for (int i = 0; i < dataGridView4.RowCount; i++)
            {
                string stLine = "";
                for (int j = 0; j < dataGridView4.Rows[i].Cells.Count; j++)
                    stLine = stLine.ToString() + Convert.ToString(dataGridView4.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();
        }
        private void button6_Click(object sender, EventArgs e)
        {

            string months = dateTimePicker1.Value.ToString("MMMM");
            string Years = dateTimePicker1.Value.ToString("yyyy");
                ToCsV(dataGridView1, @"D:\"+months+""+" "+""+Years+".xls")
}

推荐答案

调试器是你的朋友。



The debugger is your friend.

Debug.WriteLine( sHeaders );


这篇关于我想在excel文件中导出带有整个标头的datagridview值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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