2

我有这个在 Excel 表中导出 datagridView 的函数:

 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 打开,给我一个导入屏幕,我无法打开电子表格。

为什么 ?如何导出与 openoffice calc 兼容的 Excel 工作表?

4

3 回答 3

1

我想你有,但我想知道你是否考虑过导出为 CSV 格式。OpenOffice 和 Excel(以及任何其他可以导入 CSV 的文件)都可以打开该文件,但您可能会丢失一些格式选项。

于 2010-09-10T15:30:07.237 回答
1

首先,您似乎将 xml 文件另存为 xls。Microsoft Excel 可以打开 xml 文件,而Calc可能不能。我建议您使用Excel 对象创建 excelsheet ,然后尝试在 Calc 中打开它。这个例子可能会有所帮助。

于 2010-09-10T15:36:05.863 回答
0

请找到您需要关闭 Filestream fs.close() 的工作代码

StreamWriter fs = new StreamWriter(Application.StartupPath + "\Export.xls", 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 <= dgvReport.Columns.Count - 1; x++)
                    {
                        fs.WriteLine("            <ss:Column ss:Width=\"{0}\"/>", dgvReport.Columns[x].Width);
                    }
                    fs.WriteLine("            <ss:Row ss:StyleID=\"1\">");
                    for (int i = 0; i <= dgvReport.Columns.Count - 1; i++)
                    {
                        fs.WriteLine("                <ss:Cell>");
                        fs.WriteLine(string.Format("                   <ss:Data ss:Type=\"String\">{0}</ss:Data>", dgvReport.Columns[i].HeaderText));
                        fs.WriteLine("                </ss:Cell>");
                    }
                    fs.WriteLine("            </ss:Row>");
                    for (int intRow = 0; intRow <= dgvReport.RowCount - 2; intRow++)
                    {
                        fs.WriteLine(string.Format("            <ss:Row ss:Height =\"{0}\">", dgvReport.Rows[intRow].Height));
                        for (int intCol = 0; intCol <= dgvReport.Columns.Count - 1; intCol++)
                        {
                            fs.WriteLine("                <ss:Cell>");
                            fs.WriteLine(string.Format("                   <ss:Data ss:Type=\"String\">{0}</ss:Data>", (dgvReport.Rows[intRow].Cells[intCol].Value != null) ? dgvReport.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>");
                    fs.Close();
于 2015-04-01T07:23:21.013 回答