Excel模版页面打印

Excel模版页面打印

   在做web项目的时候,经常碰到打印;我们通常的办法是做一个打印页面,然后在打印,或许通过第三方控件直接调用打印;然而有的客户需要有打印的同时还需要导出Excel,那我们怎样保证两种方式的结果一样漂亮呢?下面就是用Excel模版做的Web打印。

代码:

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.IO;

using SpreadsheetGear;

using System.Web.Hosting;

 

/// <summary>

///ExcelWebPrintHelp 的摘要说明

/// </summary>

public class ExcelWebPrintHelp

{

    private static WriteToTemplateExcel _wExcel = new WriteToTemplateExcel();

    private static string _defaultTempFile = Path.Combine(System.Web.HttpContext.Current.Request.PhysicalApplicationPath, "ERPB/uploadFile/tempfile");

 

    /// <summary>

    /// 默认临时文件夹

    /// </summary>

    public static string DefaultTempFile

    {

        get

        {

            return _defaultTempFile;

        }

        set

        {

            _defaultTempFile = value;

        }

    }

 

    private static string getUserFilePath()

    {

        string path = Path.Combine(_defaultTempFile, ERP_corefun.usercore.getSessionUserInfo().ID.ToString());

        return path;

    }

 

 

    public ExcelWebPrintHelp()

    {

        //

        //TODO: 在此处添加构造函数逻辑

        //

    }

    

    /// <summary>

    /// WEB形式打印EXCEL工作簿

    /// </summary>

    /// <param name="page">当前页面,一般传入this</param>

    /// <param name="workBook">Excel需要打印的Workbook</param>

    public static void PrintExcelWorkbook(Page page, IWorkbook workBook)

    {

        string fileName = getUserFilePath() + ".xls";

        if (File.Exists(fileName))

        {

            File.Delete(fileName);

        }

        workBook.SaveAs(fileName, FileFormat.XLS97);

        excelChangeToMht(fileName);

        string webFile = ConvertSpecifiedPathToRelativePath(page, fileName.Replace(".xls", ".mht"));

        page.Response.Redirect(webFile);

 

    }

 

    /// <summary>

    /// 绝对路径转换为相对路径

    /// </summary>

    /// <param name="page"></param>

    /// <param name="specifiedPath"></param>

    /// <returns></returns>

    public static string ConvertSpecifiedPathToRelativePath(Page page, string specifiedPath)

    {

        string virtualPath = page.Request.ApplicationPath;

 

        string pathRooted = HostingEnvironment.MapPath(virtualPath);

 

        if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)

        {

            return specifiedPath;

        }

 

 

        if (pathRooted.Substring(pathRooted.Length - 1, 1) == "\\")

        {

            specifiedPath = specifiedPath.Replace(pathRooted, "~/");

        }

        else

        {

 

            specifiedPath = specifiedPath.Replace(pathRooted, "~");

        }

 

        string relativePath = specifiedPath.Replace("\\", "/");

        return relativePath;

    }

 

    //Excel转换为MHT

    private static void excelChangeToMht(string fileName)

    {

        

        _wExcel.Open(fileName);

        string extensionName = System.IO.Path.GetExtension(fileName);

        string mhtFile = fileName.Replace(extensionName, "_1.mht");

        if (File.Exists(mhtFile))

        {

            File.Delete(mhtFile);

        }

        _wExcel.SaveMhtFile(mhtFile);

        _wExcel.Dispose();

        File.Delete(fileName);

        StreamReader objReader = new StreamReader(mhtFile);

        string newMhtFile = mhtFile.Replace("_1.mht", ".mht");

        if (File.Exists(newMhtFile))

        {

            File.Delete(newMhtFile);

        }

        FileStream fs = new FileStream(newMhtFile, FileMode.Create);

        StreamWriter sw = new StreamWriter(fs);

        string sLine = "";

 

        int htmlIndex = 0;

 

        while (!objReader.EndOfStream)

        {

            sLine = objReader.ReadLine();

            if (sLine != null)

            {

                if (sLine.IndexOf("</html>") > -1)

                {

                    htmlIndex++;

                    if (htmlIndex == 5)

                    {

                        sw.WriteLine("<script>window.print();</script>");

                    }

                }

            }

            sw.WriteLine(sLine);

        }

        objReader.Close();

        sw.Close();

        fs.Close();

        File.Delete(mhtFile);

    }

}

 

posted @ 2013-06-20 10:06  greefsong  阅读(347)  评论(0编辑  收藏  举报