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

  • 相关阅读:
    ceph 网络配置
    Centos7.2 下DNS+NamedManager高可用部署方案完整记录
    Mysql多实例数据库
    Mysql 基础
    搭建本地YUM仓库
    Go实现线程安全的缓存
    KubeEdge安装详细教程
    Kubeedge实现原理
    Go语言中new()和make()的区别
    Go语言中append()函数的源码实现在哪里?
  • 原文地址:https://www.cnblogs.com/sczw-maqing/p/3365856.html
Copyright © 2011-2022 走看看