小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

NPOI 2.0 Excel讀取顯示

 昵稱10504424 2014-03-14

最近接到需求,需要把excel表格里的數(shù)據(jù)原樣展示到web頁面,主要是滿足隨意跨行跨列。

之前用過一點NPOI,不過接觸的不太多,趁這次機會再熟悉一下。由于操作的excel都是2007以上的版本,所以選擇了2.0的版本。

這里稍微提一下2.0與1.X的區(qū)別:2.0主要針對2007及以上版本,1.X主要針對2003,此外方法也略有不同,但是過渡還是很平緩的,這里不做過多的贅述。

詳情請看官網(wǎng):點擊此處

假設一下是excel 文件的 Sheet1頁,轉(zhuǎn)換成web之后仍是同樣效果。

日期買入
買入額(萬元)償還額(萬元)凈買入額(萬元)
2014-1-3067644.7158602.779041.94
2013-12-31520660.88449425.2271235.66
2013-11-29515912.92525626.82-9713.91
2013-10-31758822.25738848.4719973.79

后臺代碼:

復制代碼

    using NPOI;
    using NPOI.HSSF.UserModel;   //2003版本
    using NPOI.XSSF.UserModel;   //2007版本
    using NPOI.SS.UserModel;



public string ConvertExcelToJsonString() { try {
         string excelName = "data.xlsx";
string sheet = "Sheet3"; string filePath = HttpContext.Current.Server.MapPath(String.Format("~/App_Data/{0}", excelName)); //HSSFWorkbook wb = new HSSFWorkbook(new FileStream(filePath, FileMode.Open, FileAccess.Read)); //HSSFSheet sht = (HSSFSheet)wb.GetSheet(sheet); 如果是2003 則用HSS開頭的對象。 FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read); XSSFWorkbook xworkbook = new XSSFWorkbook(file); XSSFSheet xsheet = (XSSFSheet)xworkbook.GetSheet(sheet); int rowsCount = xsheet.PhysicalNumberOfRows; //取行Excel的最大行數(shù) int colsCount = xsheet.GetRow(0).PhysicalNumberOfCells;//取得Excel的列數(shù) StringBuilder excelJson = new StringBuilder(); //StringBuilder table = new StringBuilder(); int colSpan; int rowSpan; bool isByRowMerged; excelJson.Append("["); //table.Append("<table border='1px'>"); for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++) { if (rowIndex > 0) { excelJson.Append(","); } excelJson.Append("["); for (int colIndex = 0; colIndex < colsCount; colIndex++) { //獲取Table某個TD合并的列數(shù)和行數(shù)等信息。與Excel中對應Cell的合并行數(shù)和列數(shù)一致。 GetTdMergedInfo(xsheet, rowIndex, colIndex, out colSpan, out rowSpan, out isByRowMerged); //被合并的行或列不輸出的處理方式不一樣 //如果已經(jīng)被 行 合并包含進去了就不輸出TD了。 if (isByRowMerged) { continue; } excelJson.Append("{"); excelJson.Append(string.Format("Text:'{0}'", xsheet.GetRow(rowIndex).GetCell(colIndex))); excelJson.Append(string.Format(",ColSpan:'{0}'",colSpan)); excelJson.Append(string.Format(",RowSpan:'{0}'",rowSpan)); excelJson.Append(",Width:''"); excelJson.Append(",Align:'center'"); excelJson.Append(",Index:''"); excelJson.Append("},"); //列被合并之后此行將少輸出colSpan-1個TD。 if (colSpan > 1) colIndex += colSpan - 1; } excelJson.Remove(excelJson.Length-1,1); excelJson.Append("]"); } excelJson.Append("]"); return excelJson.toString(); } catch (Exception ex) { return null; } }
復制代碼

前臺代碼:

以上功能是將 excel 里的數(shù)據(jù)轉(zhuǎn)化成json格式(如下),因為還有別的用處,所以就沒直接轉(zhuǎn)換成Html的table。如果想直接轉(zhuǎn)換成Table,請參考官網(wǎng)例子

復制代碼
復制代碼

以上僅作為學習資料,方便自己以后查找,寫的不是很詳細,如有疑問可以留言。謝謝!

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多