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>");
}
}
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 ?
First off, it seems that you are saving an xml file as xls. Microsoft Excel can open the xml files, whereas probably Calc cannot. I’d suggest you create excelsheet using Excel object and then try opening it in Calc. This example might help.