网站首页 博客 C#将Excel转换成图片
C#将Excel转换成图片
 2017-05-26 16:44:44  管理员  1341

由于需要用到Aspose.Cells.dll,所以先添加引用

QQ截图20170526160949.png

属性和函数定义

private string current_fullpath = ""; //文件完整路径
private string current_dirname = ""; //文件所在目录
private string current_filename = ""; //文件名(不包含后缀)
private string current_fileext = ""; //文件后缀
private int current_totalpage = 0; //转换后的图片数量
private string office2ext = "png"; //转换后的图片保存类型
private string logtext; //日志信息

//将Excel转换成图片
private void ConvertExcel2Image()
{
    Aspose.Cells.Workbook excel = null;
    try
    {
        excel = new Aspose.Cells.Workbook(this.current_fullpath);
    }
    catch (Exception e)
    {
        this.logtext = ", " + e.Message + "。";
        messagetextBox.AppendText(this.logtext);
        messagetextBox.ScrollToCaret();
    }
    if (excel != null)
    {
        try
        {
            excel.Save(this.current_dirname + "\\" + this.current_filename + ".pdf", Aspose.Cells.SaveFormat.Pdf);
            this.current_fullpath = this.current_dirname + "\\" + this.current_filename + ".pdf";
            this.current_fileext = "pdf";
            this.ConvertPdf2Image();
        }
        catch (Exception e)
        {
            this.logtext = ", " + e.Message + ", 转成pdf时失败。";
            messagetextBox.AppendText(this.logtext);
            messagetextBox.ScrollToCaret();
        }
    }
    else
    {
        this.logtext = ", 文件无效或者被加密。";
        messagetextBox.AppendText(this.logtext);
        messagetextBox.ScrollToCaret();
    }
}

调用方法

this.current_fullpath = "E:/file/test4.xlsx"; //文件完整路径
this.current_fullpath = Path.GetFullPath(this.current_fullpath.Replace("/", "\\\\")); //斜杠替换
this.current_dirname = Path.GetDirectoryName(this.current_fullpath); //文件所在目录
this.current_filename = Path.GetFileNameWithoutExtension(this.current_fullpath); //文件名(不包含后缀)
this.current_fileext = "xlsx"; //文件后缀
this.ConvertExcel2Image();

注意事项

将excel转换成图片不是一步完成的,中间先转换成了pdf,然后再转换成的图片。

将pdf转换成图片的方法详见这里


来说两句吧
最新评论