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

        }

    }

  • 相关阅读:
    Kubernetes(十一) 部署doshboard
    kubernetes(一)kubeadm安装
    kubernetes安装-二进制
    使用Jmeter+Maven+Jenkins实现接口自动化测试
    使用Jmeter在linux环境实现分布式负载
    Jmeter连接Mysql和Oracle数据库
    Jmeter如何实现参数化用户,并且管理Cookie
    开启MYSQL慢查询日志,监控有效率问题的SQL
    使用jmeter+ant+jenkins实现接口自动化测试
    使用Jmeter对SHA1加密接口进行性能测试
  • 原文地址:https://www.cnblogs.com/greefsong/p/3145464.html
Copyright © 2020-2023  润新知