开发环境是Lab Windows CVI,需要通过代码打开EXCEL,并导出该EXCEL中的几张图片到指定文件夹中。需要实现这个功能的代码。
using Microsoft.Office.Interop.Excel;
public static bool Convert(string sourcePath, string targetPath, XlFixedFormatType targetType)
{
bool result;
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Application application = null;
Workbook workBook = null;
try
{
application = new Microsoft.Office.Interop.Excel.Application();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
static void Main(string[] args)
{
string excelfilePath = ReadIni(@"c:\\file_important\\inicfg.ini", "INFO_R", "epath", "DefaultName");
string pdffilePath = ReadIni(@"c:\\file_important\\inicfg.ini", "INFO_R", "ppath", "DefaultName");
// bool result = Program.Convert("C:\\report.xls", "C:\\测试报告", XlFixedFormatType.xlTypePDF);
bool result = Program.Convert(excelfilePath, pdffilePath, XlFixedFormatType.xlTypePDF);
//如果转换成功
if (result)
{
}
}