将xml类型的excel文件转换为标准的Excel格式文件
·
工作原理是,如双击打开Xml表格的Excel文件另存为Microsoft Office Excel 工作薄,代码是使用了系统安装的office插件的Microsoft.Office.Interop将xml文件转换为标准的Excel格式文件,具体代码如下:
/// <summary>
/// 将xml类型的excel文件转换为标准的Excel格式文件
/// </summary>
/// <param name="strpath">xml类型的excel文件物理路径</param>
/// <param name="filePathName">标准的Excel格式文件物理路径</param>
/// <returns>成功true 失败false</returns>
public static bool XmlToExcel(string strpath, string filePathName)
{
try
{
//将xml文件转换为标准的Excel格式 //由于yongCOM组件很多值需要用Missing.Value代替
Object Nothing = System.Reflection.Missing.Value;
// 初始化
Microsoft.Office.Interop.Excel.Application ExclApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
//打开Excl工作薄
Microsoft.Office.Interop.Excel.Workbook ExclDoc = ExclApp.Workbooks.Open(strpath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
try
{
//获取Excl 2007文件格式
Object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal;
ExclApp.DisplayAlerts = false;
//保存为Excl 2007格式
ExclDoc.SaveAs(filePathName, format, Nothing, Nothing, Nothing, Nothing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
}
catch (Exception ex) { return false; }
ExclDoc.Close(Nothing, Nothing, Nothing);
ExclApp.Quit();
}
catch (Exception e)
{
return false;
}
return true;
}
如果报错:错误信息:检索 COM 类工厂中 CLSID 为{00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误:80070005 拒绝访问。
请访问此地址有解决方法 https://blog.csdn.net/ttbat/article/details/116855096
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)