就是在用Aspose.cells将excel转换为图片时,如果excel列数过多,生成的图片并不是所有的内容,而是一部分。求解。。。这是什么原因?还有就是excel默认3个sheet,在转图片的时候如果该sheet没有内容可以怎样判断一下?而让这个空白页不进行转换操作?
/// <summary>
/// 将Excel文档转换为图片的方法(该方法基于第三方DLL),你可以像这样调用该方法:
/// ConvertExcelImage("F:\\PdfFile.doc", "F:\\", "ImageFile", 1, 20, ImageFormat.Png, 256);
/// </summary>
/// <param name="pdfInputPath">Excel文件路径</param>
/// <param name="imageOutputPath">图片输出路径,如果为空,默认值为Excel所在路径</param>
/// <param name="imageName">图片的名字,不需要带扩展名,如果为空,默认值为Excel的名称</param>
/// <param name="startPageNum">从Excel文档的第几页开始转换,如果为0,默认值为1</param>
/// <param name="endPageNum">从Excel文档的第几页开始停止转换,如果为0,默认值为Excel总页数</param>
/// <param name="imageFormat">设置所需图片格式,如果为null,默认格式为PNG</param>
/// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
public static void ConvertExcelToImage(string excelInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, float resolution)
{
try
{
// open Excel file
Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook(excelInputPath);
int cnt = excel.Worksheets.Count;
Aspose.Cells.Rendering.ImageOrPrintOptions imageopt = new Aspose.Cells.Rendering.ImageOrPrintOptions();
imageopt.ImageFormat = imageFormat;
imageopt.OnePagePerSheet = true;
imageopt.PrintingPage = Aspose.Cells.PrintingPageType.IgnoreBlank;
// validate parameter
if (excel == null) { throw new Exception("Excel文件无效或者Excel文件被加密!"); }
if (imageOutputPath.Trim().Length == 0) { imageOutputPath = Path.GetDirectoryName(excelInputPath); }
if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
if (imageName.Trim().Length == 0) { imageName = Path.GetFileNameWithoutExtension(excelInputPath); }
if (startPageNum <= 0) { startPageNum = 1; }
if (imageFormat == null) { imageFormat = ImageFormat.Png; }
if (resolution <= 0) { resolution = 128; }
ImageSaveOptions imageSaveOptions = new ImageSaveOptions(GetSaveFormat(imageFormat));
imageSaveOptions.Resolution = resolution;
// start to convert each page
for (int j = 0; j < cnt; j++)
{
Aspose.Cells.Worksheet sheet = excel.Worksheets[j];
Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imageopt);
sheet.PageSetup.BottomMargin = 0;
sheet.PageSetup.RightMargin = 0;
sheet.PageSetup.TopMargin = 0;
sheet.PageSetup.LeftMargin = 0;
if (endPageNum > sr.PageCount || endPageNum <= 0) { endPageNum = sr.PageCount; }
if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
//for (int i = startPageNum; i <= endPageNum; i++)
//{
imageSaveOptions.PageIndex = j - 1;
string strNumber;
if (j < 10)
{
strNumber = "0" + j.ToString();
}
else
{
strNumber = j.ToString();
}
//Bitmap bit = sr.ToImage(j);
//if(bit!=null)
//bit.Save(Path.Combine(imageOutputPath, imageName) + "sheet" + j + "_" + strNumber + "." + imageFormat.ToString(), imageFormat);
sr.ToImage(j, Path.Combine(imageOutputPath, imageName) +"sheet"+j+ "_" + strNumber + "." + imageFormat.ToString());
//doc.Save(Path.Combine(imageOutputPath, imageName) + "_" + strNumber + "." + imageFormat.ToString(), imageSaveOptions);
//}
}
}
catch (Exception ex)
{
throw ex;
}
}
楼主解决了吗?我也有这种问题不知道怎么弄,求帮助
我是转换成PDF,但是excel太宽导致PDF分页显示了,跟你的情况差不多
可以试试Spire.XLS进行转换
using Spire.Xls;
using System.Drawing;
namespace Excel2Image
{
class Program
{
static void Main(string[] args)
{
//加载Excel文件
Workbook wb = new Workbook();
wb.LoadFromFile("ExcelFile.xlsx");
//遍历工作表
foreach (Worksheet sheet in wb.Worksheets)
{
//判断工作表是不是非空
if (!sheet.IsEmpty)
{
//将非空的工作表转换为图片
Image image = sheet.ToImage(1, 1, sheet.LastRow, sheet.LastColumn);
image.Save(string.Format("sheet-{0}.png", sheet.Index));
}
}
}
}
}