[C#]在xls中为openOffice Calc导出DatagridView

时间:2010-09-10 15:07:10

标签: c# excel export export-to-excel openoffice-calc

我有这个函数在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打开,请给我一个导入屏幕,我无法打开spredsheet。

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分: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();