【发布时间】:2021-03-24 13:00:30
【问题描述】:
所以我正在尝试编写一个函数来将工作簿中某些工作表的打印区域转换为图像文件。
这些工作表包含一些图表,在一些图表上,图像显示的图例如下所示:
当我在 Excel 中打开它时,它显示正常。另外值得一提的是,我在匈牙利使用 Excel,但是当转换为图片时,某些月份在其他图表上以英文显示。
C# 控制台应用程序的参考:
using System;
using xls = Microsoft.Office.Interop.Excel;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;
public static bool makeimage(string excelpath,string savefolder) {
bool ret = true;
string fileNameToProcess = excelpath;
xls.Application oExcel = new xls.Application();
xls.Workbook wb = null;
try
{
wb = oExcel.Workbooks.Open(fileNameToProcess.ToString(), false, true, Type.Missing, "", "", true, xls.XlPlatform.xlWindows, "", false, false, 0, false, true, 0);
wb.RefreshAll();
xls.Sheets sheets = wb.Worksheets as xls.Sheets;
for (int i = 1; i <= sheets.Count; i++)
{
xls.Worksheet sheet = sheets[i];
if (sheet.Visible == 0) { continue; }
xls.Range range = null;
Console.WriteLine(sheet.Name);
try
{
range = sheet.Range[sheet.PageSetup.PrintArea];
sheet.Activate();
sheet.Calculate();
sheet.Evaluate(range);
oExcel.ActiveWindow.View = xls.XlWindowView.xlNormalView;
}
catch (Exception e)
{
continue;
}
Console.WriteLine("Converting range to image: " + range.CopyPicture(xls.XlPictureAppearance.xlScreen, xls.XlCopyPictureFormat.xlBitmap).ToString());
if (Clipboard.ContainsImage())
{
string imagepath = savefolder + sheet.Name + ".PNG";
if (System.IO.File.Exists(imagepath))
System.IO.File.Delete(imagepath);
Image imgRange1 = Clipboard.GetImage();
imgRange1.Save(imagepath, System.Drawing.Imaging.ImageFormat.Png);
}
sheets[i].Delete();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ret = false;
//throw;
}
finally
{
wb.Close(false);
oExcel.Quit();
oExcel = null;
}
return ret;
}
当我尝试将这些工作表转换为 PDF 时出现同样的问题。
public static bool makePDF(string excelpath, string savefolder)
{
bool ret = true;
string fileNameToProcess = excelpath;
//Start Excel and create a new document.
xls.Application oExcel = new xls.Application();
xls.Workbook wb = null;
try
{
wb = oExcel.Workbooks.Open(fileNameToProcess.ToString(), false, true, Type.Missing, "", "", true, xls.XlPlatform.xlWindows, "", false, false, 0, false, true, 0);
wb.RefreshAll();
xls.Sheets sheets = wb.Worksheets as xls.Sheets;
for (int i = 1; i <= sheets.Count; i++)
{
xls.Worksheet sheet = sheets[i];
if (sheet.Visible == 0) { continue; }
xls.Range range = null;
Console.Write(sheet.Name);
try
{
range = sheet.Range[sheet.PageSetup.PrintArea];
//Console.WriteLine(range.Address.ToString());
sheet.Activate();
sheet.Calculate();
sheet.Evaluate(range);
oExcel.ActiveWindow.View = xls.XlWindowView.xlNormalView;
sheet.ExportAsFixedFormat(xls.XlFixedFormatType.xlTypePDF, savefolder+sheet.Name, xls.XlFixedFormatQuality.xlQualityStandard, false, false, 1, 1, false);
Console.WriteLine(" has been converted");
}
catch (Exception e)
{
Console.WriteLine(" has not been converted");
continue;
}
//}
sheets[i].Delete();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ret = false;
//throw;
}
finally
{
wb.Close(false);
oExcel.Quit();
oExcel = null;
}
return ret;
}
有人对我缺少什么或我可以尝试什么有任何建议吗?
【问题讨论】: