zoukankan      html  css  js  c++  java
  • 使用 EPPlus,NPOI,操作EXCEL

    NPOI,       读取xls文件(Excel2003及之前的版本)   (NPOI.dll+Ionic.Zip.dll)     http://npoi.codeplex.com/

    EPPlus,    读取xlsx文件(Excel2007版本)                (EPPlus.dll)                       http://epplus.codeplex.com/

    本文中只实现了Excel文件的读取,实际上,这两个控件均支持对其内容,格式,公式等进行修改,这些复杂功能尚无需求,所以没有实现

    读取接口IExcel:

    Code

    xls文件实现:

    Code

    xlsx文件实现:

    Code

    调用类:

    Code

    调用:

    ExcelLib.IExcel tmp = ExcelLib.ExcelLib.GetExcel(Application.StartupPath + "\TestUnicodeChars.xls");
    //ExcelLib.IExcel tmp = ExcelLib.ExcelLib.GetExcel(Application.StartupPath + "\TestUnicodeChars.xlsx");
    if (tmp == null) MessageBox.Show("打开文件错误");
    try
    {
    if (!tmp.Open())
        MessageBox.Show("打开文件错误");
         tmp.CurrentSheetIndex = 1;
    int asdf = tmp.GetColumnCount();
    string sdf = tmp.GetCellValue(0,1);
        tmp.Close();
    }
    catch (Exception ex)
    { MessageBox.Show(ex.Message); return

                    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Fix Asset");

                    //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
                    //ws.Cells["A1"].LoadFromDataTable(tbl, true);
                    ws.Cells["A1"].LoadFromCollection(assets, true);//collection型数据源

                    //写到客户端(下载)       
                    Response.Clear();       
                    Response.AddHeader("content-disposition", "attachment;  filename=FixAsset.xlsx");       
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    Response.BinaryWrite(pck.GetAsByteArray());       
                    //ep.SaveAs(Response.OutputStream);    第二种方式       
                    Response.Flush();
                    Response.End();  

    注意:如果是在ASCX中调用,需要:

    在Page_Load中注册两行Javascript脚本,string script = “_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper = true;”; 

    this.ClientScript.RegisterClientScriptBlock(this.GetType(), “script”, script, true); 

    可以查看:http://www.cnblogs.com/ceci/archive/2012/09/05/2671538.html

  • 相关阅读:
    3503: [Cqoi2014]和谐矩阵
    2734: [HNOI2012]集合选数
    P3900 [湖南集训]图样图森破
    4557: [JLoi2016]侦察守卫
    牛客OI周赛6-提高组 B 践踏
    连续区间的最大公约数
    Wannafly挑战赛5 D. 子序列
    牛客国庆集训派对Day1 B. Attack on Titan
    4538: [Hnoi2016]网络
    [SHOI2015]超能粒子炮·改
  • 原文地址:https://www.cnblogs.com/sczw-maqing/p/3365856.html
Copyright © 2011-2022 走看看