Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
[C#]在xls中为openOffice Calc导出DatagridView_C#_Excel_Export_Export To Excel_Openoffice Calc - Fatal编程技术网

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

[C#]在xls中为openOffice Calc导出DatagridView,c#,excel,export,export-to-excel,openoffice-calc,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("<

我有一个在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>");
            }
}
public void ExportGridToExcel(datagrid查看网格,字符串文件名)
{
使用(System.IO.StreamWriter fs=new System.IO.StreamWriter(文件名,false))
{
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);

对于(int x=0;x我想您已经考虑过了,但我想知道您是否考虑过导出为CSV格式。OpenOffice和Excel(以及任何其他可以导入CSV的文件)然后将能够打开该文件,尽管您可能会丢失一些格式选项。

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

请发现这是可行的关闭文件流fs.close()所需的代码

StreamWriter fs=新建StreamWriter(Application.StartupPath+“\Export.xls”,false)

fs.WriteLine(“”);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);
财政司司长书面资料(“);

对于(int x=0;x是,但要使用Excel对象,我需要在用户计算机上安装Office,如何检查Office是否安装在用户计算机上?@aleroot:不一定,如果在项目输出中包含引用的程序集,则不需要在用户计算机上安装Office。不过,CSV可能更容易如果您需要excel和calc的兼容性,请选择。不,您需要在每台计算机上安装excel,否则会在COM组件上出错。。。
                    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();