分享

NPOI 2.0 Excel读取显示

 昵称10504424 2014-03-14

最近接到需求,需要把excel表格里的数据原样展示到web页面,主要是满足随意跨行跨列。

之前用过一点NPOI,不过接触的不太多,趁这次机会再熟悉一下。由于操作的excel都是2007以上的版本,所以选择了2.0的版本。

这里稍微提一下2.0与1.X的区别:2.0主要针对2007及以上版本,1.X主要针对2003,此外方法也略有不同,但是过渡还是很平缓的,这里不做过多的赘述。

详情请看官网:点击此处

假设一下是excel 文件的 Sheet1页,转换成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的最大行数 int colsCount = xsheet.GetRow(0).PhysicalNumberOfCells;//取得Excel的列数 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合并的列数和行数等信息。与Excel中对应Cell的合并行数和列数一致。 GetTdMergedInfo(xsheet, rowIndex, colIndex, out colSpan, out rowSpan, out isByRowMerged); //被合并的行或列不输出的处理方式不一样 //如果已经被 行 合并包含进去了就不输出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 里的数据转化成json格式(如下),因为还有别的用处,所以就没直接转换成Html的table。如果想直接转换成Table,请参考官网例子

复制代码
[[{Text:'日期',ColSpan:'1',RowSpan:'2',Width:'',Align:'center',Index:''},{Text:'买入',ColSpan:'3',RowSpan:'1',Width:'',Align:'center',Index:''}],[{Text:'买入额(万元)',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'偿还额(万元)',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'净买入额(万元)',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''}],[{Text:'30-一月-2014',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'67644.71',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'58602.77',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'9041.94',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''}],[{Text:'31-十二月-2013',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'520660.88',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'449425.22',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'71235.66',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''}],[{Text:'29-十一月-2013',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'515912.92',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'525626.82',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'-9713.91',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''}],[{Text:'31-十月-2013',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'758822.25',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'738848.47',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''},{Text:'19973.79',ColSpan:'1',RowSpan:'1',Width:'',Align:'center',Index:''}]]
复制代码

以上仅作为学习资料,方便自己以后查找,写的不是很详细,如有疑问可以留言。谢谢!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多