将datagridview导出到xls文件 [英] export datagridview to xls file

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

问题描述

我具有在Excel工作表中导出datagridView的此功能:

I have this function that export a datagridView in Excel sheet :

public void ExportGridToExcel(DataGridView TheGrid, string FileName)
    {

            using (System.IO.StreamWriter fs = new System.IO.StreamWriter(FileName, false))
            {
                fs.WriteLine("<?xml version=\"1.0\"?>");
                fs.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
                fs.WriteLine("<ss:Workbook xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");
                fs.WriteLine("    <ss:Styles>");
                fs.WriteLine("        <ss:Style ss:ID=\"1\">");
                fs.WriteLine("           <ss:Font ss:Bold=\"1\"/>");
                fs.WriteLine("        </ss:Style>");
                fs.WriteLine("    </ss:Styles>");
                fs.WriteLine("    <ss:Worksheet ss:Name=\"Sheet1\">");
                fs.WriteLine("        <ss:Table>");
                for (int x = 0; x <= TheGrid.Columns.Count - 1; x++)

                {

                    fs.WriteLine("            <ss:Column ss:Width=\"{0}\"/>", TheGrid.Columns[x].Width);
                }
                fs.WriteLine("            <ss:Row ss:StyleID=\"1\">");
                for (int i = 0; i <= TheGrid.Columns.Count - 1; i++)

                {

                    fs.WriteLine("                <ss:Cell>");
                    fs.WriteLine(string.Format("                   <ss:Data ss:Type=\"String\">{0}</ss:Data>", TheGrid.Columns[i].HeaderText));
                    fs.WriteLine("                </ss:Cell>");
                }
                fs.WriteLine("            </ss:Row>");
                for (int intRow = 0; intRow <= TheGrid.RowCount - 2; intRow++)

                {

                    fs.WriteLine(string.Format("            <ss:Row ss:Height =\"{0}\">", TheGrid.Rows[intRow].Height));
                    for (int intCol = 0; intCol <= TheGrid.Columns.Count - 1; intCol++)

                    {

                        fs.WriteLine("                <ss:Cell>");
                        fs.WriteLine(string.Format("                   <ss:Data ss:Type=\"String\">{0}</ss:Data>", (TheGrid.Rows[intRow].Cells[intCol].Value != null) ? TheGrid.Rows[intRow].Cells[intCol].Value.ToString() : string.Empty));
                        fs.WriteLine("                </ss:Cell>");
                    }
                    fs.WriteLine("            </ss:Row>");
                }
                fs.WriteLine("        </ss:Table>");
                fs.WriteLine("    </ss:Worksheet>");
                fs.WriteLine("</ss:Workbook>");
            }
}


如果我尝试使用Microsoft Excel打开生成的XLS,则一切正常,但如果尝试使用OpenOffice Calc打开,请给我一个导入屏幕,但我无法打开spredsheet.

为什么 ?如何导出与openoffice calc兼容的Excel工作表?
我想在ms office或open office中执行xls文件,它应该可以正常工作.而无需添加com组件.我正在使用.net Framework 2.0和Server 2005can任何帮助


If i try to open the generated XLS with Microsoft Excel everything is ok, but if try to open with OpenOffice Calc, give me an import screen and i can''t open the spredsheet.

Why ? How can i do for export an Excel sheet compatibile with openoffice calc ?
I want to exceute the xls file in both either ms office or open office it should work.with out adding com component. I am using .net framework 2.0 and server 2005can any help

推荐答案

您好,请参阅本文:
Hi, See this article: Exporting Data to Excel[^]


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

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