aspose将excel的每一个sheet 转换为图片,缺少很多文字

bsnry 2013-01-24 04:29:29
aspose将excel的每一个sheet 转换为图片,结果缺少很多文字


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Cells;
using System.IO;

using System.Drawing.Imaging;




    class Program
{
static void Main(string[] args)
{
string excelInputPath = "e:\\1.xlsx";
string imageOutputPath = "";

if (imageOutputPath.Trim().Length == 0)
{
imageOutputPath = Path.GetDirectoryName(excelInputPath)+Path.GetFileNameWithoutExtension(excelInputPath);
}

Workbook workbook = new Workbook();
workbook.Open(excelInputPath);
int i = 0;
ImageFormat imageFormat = ImageFormat.Jpeg;
foreach (Worksheet worksheet in workbook.Worksheets)
{
if (worksheet.Cells.Rows.Count > 0)
{
worksheet.SheetToImage(imageOutputPath + "_" + i.ToString()+"." + imageFormat,
ImageFormat.Jpeg);
i++;
}
}
...全文
835 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssll7759 2013-09-10
  • 打赏
  • 举报
回复
的确,要把nPages这个参数固定为0,否则图片不会继续生成,注意后面的输出文件名中的nPages继续递增,不然后面的文件名都相同就会把前面生成的文件覆盖
bsnry 2013-01-28
  • 打赏
  • 举报
回复
bsnry 2013-01-28
  • 打赏
  • 举报
回复
bsnry 2013-01-28
  • 打赏
  • 举报
回复
引用 11 楼 shi0090 的回复:
初步是怀疑你的aspose版本问题,多找几个版本测试一下,一般官方测试版会有限制,只让你转换一页。 这里有如何把emf转化为其他格式的方法,http://social.microsoft.com/Forums/de-DE/visualcshartzhchs/thread/67d5b76b-6aef-437c-97cb-739b58a016a6
已经找到原因了 sr.ToImage(nPages 这个参数,无意发现的。 已经解决问题
劼哥stone 2013-01-28
  • 打赏
  • 举报
回复
初步是怀疑你的aspose版本问题,多找几个版本测试一下,一般官方测试版会有限制,只让你转换一页。 这里有如何把emf转化为其他格式的方法,http://social.microsoft.com/Forums/de-DE/visualcshartzhchs/thread/67d5b76b-6aef-437c-97cb-739b58a016a6
bsnry 2013-01-25
  • 打赏
  • 举报
回复
引用 7 楼 Chinajiyong 的回复:
调试看看book.Worksheets的个数 foreach (Worksheet sheet in book.Worksheets)
npages是3,也就是说明了, 执行了3次循环。 我对你提供的源码,几乎一直。 而且,我也调试过了,确实是3次。 程序没有任何异常,没有任何提示错误信息。只是有2个emf文件是空的。
EnForGrass 2013-01-25
  • 打赏
  • 举报
回复
调试看看book.Worksheets的个数 foreach (Worksheet sheet in book.Worksheets)
bsnry 2013-01-25
  • 打赏
  • 举报
回复
引用 3 楼 Chinajiyong 的回复:
参考http://www.cnblogs.com/evgetsolutions/archive/2011/09/05/2167164.html
晕倒,仔细看了转换的图片,发现这个代码,只能转换第0个worksheet,其他转换不了. 其他的图片是空的
             nPages = 0;  

             if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }

             imageOutputPath = Path.GetDirectoryName(excelInputPath) + Path.GetFileNameWithoutExtension(excelInputPath);


            //Open the template file

            Workbook book = new Workbook(excelInputPath);

            foreach (Worksheet sheet in book.Worksheets)
            {

                //Specify your print area if you want

                //sheet.PageSetup.PrintArea = "A1:H8";

                //To remove the white border around the image.

                sheet.PageSetup.LeftMargin = 0;

                sheet.PageSetup.RightMargin = 0;

                sheet.PageSetup.BottomMargin = 0;

                sheet.PageSetup.TopMargin = 0;

                //Define ImageOrPrintOptions

                ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();

                ImageFormat imageFormat = ImageFormat.Emf;

                imgOptions.ImageFormat = imageFormat;

                //Set only one page would be rendered for the image

                imgOptions.OnePagePerSheet = true;

                imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;

                //Create the SheetRender object based on the sheet with its

                //ImageOrPrintOptions attributes

                SheetRender sr = new SheetRender(sheet, imgOptions);

                //Convert the image
                sr.ToImage(nPages, imageOutputPath + "_" + nPages.ToString() + "." + imageFormat.ToString());

                nPages++;

            }
我做的excel的例子是3个worksheet,结果只有第0个worksheet被转换,按道理是应该转换出3张图片才对。 3张图片都不能使空的!!! 现在只有第0个不是空图片
bsnry 2013-01-25
  • 打赏
  • 举报
回复
引用 3 楼 Chinajiyong 的回复:
参考http://www.cnblogs.com/evgetsolutions/archive/2011/09/05/2167164.html
多谢已经解决, emf转换为jpg,能否做到不失真???? 感觉是不行了, 如果不转换,emf很大, 对于网络传输来说,非常庞大
bsnry 2013-01-24
  • 打赏
  • 举报
回复
引用 1 楼 gxingmin 的回复:
图片换其他格式试试
依然不行,我改成了png 我觉得是否 就不应该用这几个类, 谢谢诶你,前几天我问你过你托管c++ 我不懂C#, 临时学习的,项目需要的 我们的要求是: 不要依赖于安装office,就可以把office转换为图片 aspose.word可以转化图片,但是 excel似乎转化有问题。 谁能提供一个法子解决
gxingmin 2013-01-24
  • 打赏
  • 举报
回复
图片换其他格式试试

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧