js以excel为模板的打印

使用excel为模板打印的好处是格式容易调整,这种方法要求客户端系统配置高度统一,譬如excel安装版本一致,存在服务器上的excel模板必须与客户端excel版本一致,而且不能用其他版本的excel修改。

javascript打印excel代码

<script type="text/javascript" >
    function Print_Click() {
        alert("打印测试");
        var Template = "http://172.16.18.217/ExcelDoc/CreateCard.xls";
        var xlApp = new ActiveXObject("Excel.Application");
        var xlBook = xlApp.Workbooks.add(Template);
        var xlsheet = xlBook.ActiveSheet;
        xlsheet.printout;
        xlApp = null;
        xlsheet = null;

    }
</script>

ASP.NET中后台调用javascript打印excel代码

protected void PrintTestButton_Click(object sender, EventArgs e)
{
    string StrScript;
    StrScript = ("<script type='text/javascript'>");
    StrScript += ("alert(\"打印测试\");");
    StrScript += ("var Template ='http://172.16.18.217/ExcelDoc/CreateCard.xls';");
    StrScript += ("var xlApp = new ActiveXObject('Excel.Application');");
    StrScript += ("if(xlApp== undefined){");
    StrScript += ("alert(\"Excel对象创建失败\");}else{");
    StrScript += ("var xlBook = xlApp.Workbooks.add(Template);");
    StrScript += ("var xlsheet = xlBook.ActiveSheet;");
    StrScript += ("xlsheet.printout;");
    StrScript += ("xlApp = null;");
    StrScript += ("xlsheet = null;}");
    StrScript += ("</script>");
    this.ClientScript.RegisterStartupScript(this.GetType(), " ", StrScript);
}

C#打印

public int PrintPatientInfo()
{
    PrintExcel pExcel = new PrintExcel("CreateCard.xls");

    Worksheet sheet = pExcel.GetWorksheet();

    sheet.Cells[1, 1] = "建卡信息";
    sheet.Cells[2, 1] = "患者卡号:";
    sheet.Cells[2, 2] = patientCardNum.Trim();
    sheet.Cells[2, 3] = "患者姓名:";
    sheet.Cells[2, 4] = patientName.Trim();
    sheet.Cells[4, 1] = "金额";
    sheet.Cells[4, 2] = "1元";
    sheet.Cells[4, 3] = "日期";
    sheet.Cells[4, 4] = DateTime.Now.ToString("yyyy-MM-dd");
    pExcel.ExecPrint(1);

    return 0;
}

ASP.NET在服务器端用C#组织javascript打印代码并在IE客户端打印,还可以指定打印机,最方便

public class PrintExcelJs
{
    private string excelPath;
    private string StrScript;

    public PrintExcelJs(string xslPath)
    {
        excelPath = xslPath;
        StrScript = "<script type='text/javascript'>";
        StrScript += "var Template ='"+excelPath+"';";
        StrScript += "var xlApp = new ActiveXObject('Excel.Application');";
        StrScript += "if(xlApp== undefined){";
        StrScript += "alert(\"Excel对象创建失败\");}else{";
        StrScript += "var xlBook = xlApp.Workbooks.add(Template);";
        StrScript += "var xlsheet = xlBook.ActiveSheet;";
    }

    public void InsertValueToExcelCell(int i, int j, string strContent)
    {
        StrScript += "xlsheet.cells(" + i.ToString() + "," + j.ToString() + ")";
        StrScript += "=\"" + strContent + "\";";
    }
    
    public void ExecPrint(System.Web.UI.Page page,int numOfPrint=1)
    {
        StrScript += "xlsheet.printout(1,null,1,false,\"PrintGuid\",false,false);";//这段代码花了我半天时间,因为之前听说javascript不能指定打印机,存疑,后来终于找到正确的用法了
        StrScript += "xlApp = null;";
        StrScript += "xlsheet = null;}";
        StrScript += "</script>";
        page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), StrScript);
    }
}

示例代码:

private void PrintTestButton_Click()
{
    string excelPath = "http://"+Request.Url.Host + @"/ExcelDoc/" + "CreateCard.xls";
    PrintExcelJs pExcel = new PrintExcelJs(excelPath);

    pExcel.InsertValueToExcelCell(1, 1, "XX信息");
    pExcel.InsertValueToExcelCell(2, 1, "患者卡号:");
    pExcel.InsertValueToExcelCell(2, 2, patientCardNum.Trim());
    pExcel.InsertValueToExcelCell(2, 3, "患者姓名:");
    pExcel.InsertValueToExcelCell(2, 4, patientName.Trim());
    pExcel.InsertValueToExcelCell(4, 1, "金额");
    pExcel.InsertValueToExcelCell(4, 2, "1元");
    pExcel.InsertValueToExcelCell(4, 3, "日期");
    pExcel.InsertValueToExcelCell(4, 4, DateTime.Now.ToString("yyyy-MM-dd"));
    pExcel.ExecPrint(this,1);
}

 

posted @ 2015-02-05 16:49  疯狂的拖鞋  阅读(3475)  评论(0编辑  收藏  举报