使用 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

相關文章
相關標籤/搜索