使用 EPPlus,NPOI,操做EXCEL

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

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

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

读取接口IExcel:spa

 

Code

 

 

xls文件实现:code

Code

 

 

xlsx文件实现:orm

 

Code

 

 

调用类:xml

Code

 

 

调用:htm

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");blog

                //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;\n_spSuppressFormOnSubmitWrapper = true;”; 

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

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

相关文章
相关标签/搜索