该死的微软来了,被逼要使用OpenOffice。可是目前尚未见到有方法,可以把查询出的GridView的数据导出为OpenOffice支持的格式的文件,哪位高人有呀?网上搜索到一高人的帖子(http://www.liuwu.net/post/how-to-export-excel-2.aspx),可是测试后并不通过,下面我目前的函数,导出XML格式的,用微软的Excel正常打开,可是用OpenOffice无法正确打开。 private void ExportXML(string FileType, string FileName)
{
//string style = @"<style> .text { mso-number-format:\@; } </script> ";
//Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8));
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
sw.WriteLine("<?xml version=\"1.0\"?>");
sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sw.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sw.WriteLine("<Worksheet ss:Name=\"Sheet1\">");
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
//Response.Write(style); sw.WriteLine("</Worksheet>");
sw.WriteLine("</Workbook>");
Response.Write(sw);
Response.End();
}期待高人的出现。
{
//string style = @"<style> .text { mso-number-format:\@; } </script> ";
//Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8));
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
sw.WriteLine("<?xml version=\"1.0\"?>");
sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sw.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sw.WriteLine("<Worksheet ss:Name=\"Sheet1\">");
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
//Response.Write(style); sw.WriteLine("</Worksheet>");
sw.WriteLine("</Workbook>");
Response.Write(sw);
Response.End();
}期待高人的出现。
参考资料:
http://blog.csdn.net/w59879213/archive/2008/10/17/3088955.aspx
http://www.cnblogs.com/hbb0b0/archive/2009/04/14/1435762.html
http://topic.csdn.net/u/20090520/09/8e1e15dd-05f6-422e-bbb8-f4c12ede946c.html?seed=2030175578
关键代码:
public static void DataTabletoExcel(DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable != null)
{
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
Application xlApp = new ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Workbook xlBook = xlApp.Workbooks.Add(true);
Worksheet worksheet = (Worksheet) xlBook.Worksheets[1];
Range range = null;
foreach (DataColumn dc in tmpDataTable.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.Caption;
}
string[,] arr = new string[rowNum, columnNum];
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < columnNum; j++)
{
arr[i, j] = tmpDataTable.Rows[i][j].ToString();
}
}
range = (Range) worksheet.Cells[2, 1];
range.get_Resize(rowNum, columnNum).Value2 = arr;
xlBook.SaveCopyAs(strFileName);
}
}
现在正在该LZ的代码为从GridView中导出,不过尚未成功,要不LZ再伸伸援手?共同抵抗微软。
http://msdn.microsoft.com/zh-cn/library/wss56bz7(VS.80).aspx我可怜的GridView目前只是导出了表头,数据尚未导出,不过测试过,用OpenOffice可以打开,继续努力。
public static void GridViewtoExcel(GridView GV, string strFileName)
{
int rowNum = GV.Rows.Count;
int columnNum = GV.Columns.Count;
int rowIndex = 1;
int columnIndex = 0; Microsoft.Office.Interop.Excel.Application xlApp = new ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Workbook xlBook = xlApp.Workbooks.Add(true);
Worksheet worksheet = (Worksheet)xlBook.Worksheets[1];
Range range = null;