我有一个winform应用程序,我希望使用流编写器和savefiledialog导出我的数据表到office 2007(.xlsx)格式。但是当我打开excel文件时,我发现此错误:文件格式或文件扩展名无效。
Fx_3saveFileDialog.Filter =“Excel 2007-2010文件(.xlsx)| * .xlsx; Fx_3saveFileDialog.DefaultExt =”xlsx“;
if (Fx_3saveFileDialog.ShowDialog() == DialogResult.OK)
{
dsUrunTableAdapters.PF_FormDegerTableAdapter adapter = new dsUrunTableAdapters.PF_FormDegerTableAdapter();
dsUrun.PF_FormDegerDataTable degertable = new dsUrun.PF_FormDegerDataTable();
adapter.Fill(degertable);
StreamWriter writer = new StreamWriter(Fx_3saveFileDialog.FileName,true);
//StringWriter sw = new StringWriter();
//StringBuilder sb = new StringBuilder(dialog.FileName);
writer.Write("<html>");
writer.Write("<head>");
writer.Write("</head>");
writer.Write("<body>");
writer.Write("<table>");
foreach( DataColumn c in degertable.Columns )
{
//writer.Write(c.Caption);
writer.Write("<td>");
writer.Write(c.Caption);
writer.Write("</td>");
}
writer.Write("</table>");
writer.Write("</body>");
writer.Write("</html>");
//writer.WriteLine();
foreach (DataRow r in degertable.Rows)
{
//writer.Write(r[degertable.SecilebilecekDegerlerColumn].ToString());
}
writer.Close();
答案 0 :(得分:0)
Excel文件是包含开放格式的压缩文件。您正在编写HTML页面并为其提供xlsx扩展名,但这不起作用。
要么必须使用COM并操纵Excel来添加单元格,要么最简单,将表格输出为CSV文本文件(逗号分隔文件),Excel可以非常轻松地读取。