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);
}
}
}
...全文
151 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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);
}
}
}

具体内容请参考我的BLOG:http://blog.csdn.net/smallwhiteyt/archive/2009/11/08/4784771.aspx 如果你耐心仔细看完本文,相信以后再遇到出EXCLE操作的时候你会很顺手觉得SO EASY,主要给新手朋友们看的,老鸟可以直接飘过了,花了一晚上的时间写的很辛苦,如果觉得对你有帮助烦请留言支持一下,我会写更多基础的原创内容来回报大家。 C#数据EXCEL表格是个老生常谈的问题了,写这篇文章主要是给和我一样的新手朋友提供两种EXCEL的方法并探讨一下出的效率问题,本文中的代码直接就可用,其中部分代码参考其他的代码并做了修改,抛砖引玉,希望大家一起探讨,如有不对的地方还请大家多多包涵并指出来,我也是个新手,出错也是难免的。 首先先总结下自己知道的EXCEL表格的方法,大致有以下几种,有疏漏的请大家补充。 1.数据逐条逐条的写入EXCEL 2.通过OLEDB把EXCEL做为数据源来写 3.通过RANGE范围写入多行多列内存数据EXCEL 4.利用系统剪贴板写入EXCEL 好了,我想这些方法已经足够完成我们要实现的功能了,方法不在多,在精,不是么?以上4中方法都可以实现EXCEL,方法1为最基础的方法,意思就是效率可能不是太高,当遇到数据量过大时所要付出的时间也是巨大的,后面3种方法都是第一种的衍生,在第一种方法效率低下的基础上改进的,这里主要就是一个效率问题了,当然如果你数据量都很小,我想4种方法就代码量和复杂程度来说第1种基本方法就可以了,或当你的硬件非常牛逼了,那再差的方法也可以高效的完成也没有探讨的实际意义了,呵呵说远了,本文主要是在不考虑硬件或同等硬件条件下单从软件角度出发探讨较好的解决方案。

111,096

社区成员

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

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

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