WX辣乎JY 2017-02-14 03:28 采纳率: 0%
浏览 4410

Aspose.cells 在将excel转换为图片时,未显示完全

就是在用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;
            }
        }

excel:
图片说明
image:
图片说明

  • 写回答

4条回答 默认 最新

  • qq_34893536 2017-03-29 03:13
    关注

    楼主解决了吗?我也有这种问题不知道怎么弄,求帮助

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?