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

  • 相关阅读:
    ajax写用户注册
    PHP——explode的应用(获取字符串,拆为下拉列表)
    PHP 语句和时间函数
    PHP 文件上传
    PHP 文件操作代码
    PHP 弹窗 源代码 css Jquery.js
    PHP Jquery 代码操作 内容 属性 样式 事件 Json数据
    PDO 代码
    练习 《新闻管理》 添加 修改 删除
    UTF-8
  • 原文地址:https://www.cnblogs.com/sczw-maqing/p/3365856.html
Copyright © 2011-2022 走看看