Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>");

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
Updated 9-Jul-12 21:55pm

1 solution

Hi, See this article: Exporting Data to Excel[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900