C#导数据到excel的问题

木子十甫寸 2012-04-07 07:38:40
code如下,生成xlsx文件一切正常,但是xls会出现“您尝试打开的**.xls的格式与文件扩展名指定的格式不一致”。
请问这是为什么了?


private void ExtractResult_Click(object sender, EventArgs e)
{
//由于使用的是COM库,因此有许多变量需要用Nothing 代替
Object Nothing = Missing.Value;
//WdSaveFormat 为Excel文档的保存格式
object format = Excel.XlFileFormat.xlOpenXMLWorkbook;
Excel.XlFileFormat version = Excel.XlFileFormat.xlExcel8;
SaveFileDialog saveexcel = new SaveFileDialog();
saveexcel.Filter = "文件类型(*.xlsx)|*.xlsx|文件类型(*.xls)|*.xls";
if (saveexcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = saveexcel.FileName;//文件路径变量
if (System.IO.Path.GetExtension(saveexcel.FileName).ToLower() == ".xlsx")
{
version = Excel.XlFileFormat.xlOpenXMLWorkbook;
}
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
try
{
Excel.Application excelApp = new Excel.Application(); //Excel应用程序变量
Excel.Workbook excelDoc = excelApp.Workbooks.Add(Nothing); //Excel文档变量
Excel.Worksheet sheet = (Excel.Worksheet)excelDoc.Sheets[1];
sheet.Cells[1, 1] = "123";
//将 excelDoc文档对象的内容保存为XLSX文档
excelDoc.SaveAs(path, Nothing, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
//关闭excelDoc文档对象
excelDoc.Close(Nothing, Nothing, Nothing);
//关闭excelApp组件对象
excelApp.Quit();
MessageBox.Show(path.ToString() + " 导出成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
...全文
145 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ditto0723 2012-04-07
  • 打赏
  • 举报
回复
晚了?
ditto0723 2012-04-07
  • 打赏
  • 举报
回复
excelDoc.SaveAs(path, Nothing, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
改成
excelDoc.SaveAs(path, version , Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);

orochiheart 2012-04-07
  • 打赏
  • 举报
回复
恭喜 学习!
木子十甫寸 2012-04-07
  • 打赏
  • 举报
回复
搞定了,把saveas的第二个参数传version就好了
orochiheart 2012-04-07
  • 打赏
  • 举报
回复
查扩展名是否是XLS
木子十甫寸 2012-04-07
  • 打赏
  • 举报
回复
额,我也知道是格式不对.....

[Quote=引用 5 楼 的回复:]

文件格式设置的地方不对,上网查一下。
[/Quote]
木子十甫寸 2012-04-07
  • 打赏
  • 举报
回复
还是不行

[Quote=引用 4 楼 的回复:]

if (System.IO.Path.GetExtension(saveexcel.FileName).ToLower() == ".xlsx" || System.IO.Path.GetExtension(saveexcel.FileName).ToLower() == ".xls" )
{
version = Excel.XlFileFormat.xlOpenXMLWorkboo……
[/Quote]
XUKEKE2600 2012-04-07
  • 打赏
  • 举报
回复
文件格式设置的地方不对,上网查一下。
orochiheart 2012-04-07
  • 打赏
  • 举报
回复
if (System.IO.Path.GetExtension(saveexcel.FileName).ToLower() == ".xlsx" || System.IO.Path.GetExtension(saveexcel.FileName).ToLower() == ".xls" )
{
version = Excel.XlFileFormat.xlOpenXMLWorkbook;
}

没细看,,先试试
木子十甫寸 2012-04-07
  • 打赏
  • 举报
回复
周末没人上网么
木子十甫寸 2012-04-07
  • 打赏
  • 举报
回复
代码重发了一下
木子十甫寸 2012-04-07
  • 打赏
  • 举报
回复

private void ExtractResult_Click(object sender, EventArgs e)
{
//由于使用的是COM库,因此有许多变量需要用Nothing 代替
Object Nothing = Missing.Value;
//WdSaveFormat 为Excel文档的保存格式
object format = Excel.XlFileFormat.xlOpenXMLWorkbook;
Excel.XlFileFormat version = Excel.XlFileFormat.xlExcel8;
SaveFileDialog saveexcel = new SaveFileDialog();
saveexcel.Filter = "文件类型(*.xlsx)|*.xlsx|文件类型(*.xls)|*.xls";
if (saveexcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = saveexcel.FileName;//文件路径变量
if (System.IO.Path.GetExtension(saveexcel.FileName).ToLower() == ".xlsx")
{
version = Excel.XlFileFormat.xlOpenXMLWorkbook;
}
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
try
{
Excel.Application excelApp = new Excel.Application(); //Excel应用程序变量
Excel.Workbook excelDoc = excelApp.Workbooks.Add(Nothing); //Excel文档变量
Excel.Worksheet sheet = (Excel.Worksheet)excelDoc.Sheets[1];
sheet.Cells[1, 1] = "123";
//将 excelDoc文档对象的内容保存为XLSX文档
excelDoc.SaveAs(path, Nothing, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
//关闭excelDoc文档对象
excelDoc.Close(Nothing, Nothing, Nothing);
//关闭excelApp组件对象
excelApp.Quit();
MessageBox.Show(path.ToString() + " 导出成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧