Excel模版頁面打印

Excel模版頁面打印

   在作web項目的時候,常常碰到打印;咱們一般的辦法是作一個打印頁面,而後在打印,或許經過第三方控件直接調用打印;然而有的客戶須要有打印的同時還須要導出Excel,那咱們怎樣保證兩種方式的結果同樣漂亮呢?下面就是用Excel模版作的Web打印。html

代碼:web

using System;函數

using System.Data;this

using System.Configuration;excel

using System.Linq;orm

using System.Web;htm

using System.Web.Security;ip

using System.Web.UI;ci

using System.Web.UI.HtmlControls;get

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);

    }

}

相關文章
相關標籤/搜索