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

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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100