NPOI 导入 导出

孤人 提交于 2020-03-12 09:47:32

 using NPOI.XSSF.UserModel;  
using System.IO; 

导入

/// <summary>        

/// Excel转换DataTable        

/// </summary>        

/// <param name="FilePath">文件的绝对路径</param>        

/// <returns>DataTable</returns>        

public static DataTable ExcelInput(string FilePath)        

{            

//第一行一般为标题行。            

DataTable table = new DataTable();            

//根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档

 XSSFWorkbook hssfworkbook;            

using (FileStream file = new FileStream(FilePath, FileMode.Open, FileAccess.Read))            

{                

XSSFWorkbook workbook = new XSSFWorkbook(file);                

XSSFSheet sheet = (XSSFSheet)workbook.GetSheetAt(0);                

//获取excel的第一个sheet                

//获取Excel的最大行数                

int rowsCount = sheet.PhysicalNumberOfRows;  //为保证Table布局与Excel一样,这里应该取所有行中的最大列数(需要遍历整个Sheet)。                

//为少一交全Excel遍历,提高性能,我们可以人为把第0行的列数调整至所有行中的最大列数。                

int colsCount = sheet.GetRow(0).PhysicalNumberOfCells;

                for (int i = 0; i < colsCount; i++)                

{                    

table.Columns.Add(i.ToString());                

}                

for (int x = 0; x < rowsCount; x++)                

{                     DataRow dr = table.NewRow();                    

for (int y = 0; y < colsCount; y++)                    

{                        

var row = sheet.GetRow(x);                        

if (row != null)                        

{                            

var column = row.GetCell(y);                            

if (column != null)                            

{                                

dr[y] = sheet.GetRow(x).GetCell(y).ToString();                         

    }                        

}                

     }                    

table.Rows.Add(dr);           

      }

                sheet = null;                

workbook = null;          

   }           

  return table;        

}       

 

 

导出

        [Ajax]         public AjaxResult ExportWithdrawExcel(int id)         {             bool ifSuccess = false;             string msg = "";             string templatePath = Server.MapPath("\\Content\\template\\WithdrawInfo.xlsx");             string resultPath = "";             try             {                 var entity = _withdrawRequestService.GetYstObjById(id);                 WithdrawRequestModel requestModel = entity.ToModel();                 resultPath = "\\Content\\template\\" + requestModel.BillCode + "_" + DateTime.Now.Second + ".xlsx";                 string savePath = Server.MapPath(resultPath);                 IWorkbook exportFile = new XSSFWorkbook(templatePath);

                ISheet sheet1 = exportFile.GetSheet("相关提现订单");                 var list = _withdrawOrderListService.GetWithdrawOrderListByWithdrawRequestId(requestModel.Id);                 var modelList = new List<WithdrawOrderListModel>();                 foreach (var item in list)                 {                     var model = new WithdrawOrderListModel();                     if (item != null)                     {                         model = item.ToModel();                     }                     modelList.Add(model);                 }                 var gt = modelList.ToArray();

 

                System.Data.DataTable dt = IListOut(modelList);                 int rowCount = dt.Rows.Count;                 int colCount = dt.Columns.Count;                 object[,] dataArray = new object[rowCount + 1, colCount];                 for (int j = 0; j < rowCount; j++)                 {                     IRow row = sheet1.CreateRow(j + 12);                     for (int i = 0; i < colCount; i++)                     {

                        switch (dt.Columns[i].ColumnName)                         {                             case "EndDate":                                 {                                     row.CreateCell(0).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "UserName":                                 {                                     row.CreateCell(1).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "BankName":                                 {                                     row.CreateCell(2).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "OrgId":                                 {                                     row.CreateCell(3).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "CardNo":                                 {                                     row.CreateCell(4).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "Amount":                                 {                                     row.CreateCell(5).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "CreateDate":                                 {                                     row.CreateCell(6).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "WithdrawTypeShow":                                 {                                     row.CreateCell(7).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "OrderCode":                                 {                                     row.CreateCell(8).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "OrderTitle":                                 {                                     row.CreateCell(9).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "UserTypeShow":                                 {                                     row.CreateCell(10).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                             case "UniqueCode":                                 {                                     row.CreateCell(11).SetCellValue(dt.Rows[j][i].ToString());                                     break;                                 }                         }                     }                 }                 sheet1.GetRow(1).GetCell(1).SetCellValue(requestModel.BillCode.ToString());                 sheet1.GetRow(2).GetCell(1).SetCellValue(requestModel.WithdrawTypeShow.ToString());                 sheet1.GetRow(3).GetCell(1).SetCellValue(requestModel.Creator.ToString());                 sheet1.GetRow(4).GetCell(1).SetCellValue(requestModel.SubmitMan.ToString());                 sheet1.GetRow(5).GetCell(1).SetCellValue(requestModel.CheckMan.ToString());                 sheet1.GetRow(6).GetCell(1).SetCellValue(requestModel.AuditMan.ToString());                 sheet1.GetRow(7).GetCell(1).SetCellValue(requestModel.CheckMan.ToString());                 sheet1.GetRow(6).GetCell(1).SetCellValue(requestModel.Remark.ToString());                 sheet1.GetRow(1).GetCell(5).SetCellValue(requestModel.BillDate.ToString());                 sheet1.GetRow(2).GetCell(5).SetCellValue(requestModel.BillAmount.ToString());                 sheet1.GetRow(3).GetCell(5).SetCellValue(requestModel.CreatedOnUTC.ToString());                 sheet1.GetRow(4).GetCell(5).SetCellValue(requestModel.SubmitDate.ToString());                 sheet1.GetRow(5).GetCell(5).SetCellValue(requestModel.CheckDate.ToString());                 sheet1.GetRow(6).GetCell(5).SetCellValue(requestModel.AuditDate.ToString());

                FileStream sw = System.IO.File.Create(savePath);                 exportFile.Write(sw);                 sw.Close();                 ifSuccess = true;             }             catch (Exception e)             {                 msg = e.Message;                 if (e.InnerException != null)                     msg += e.InnerException.Message;             }             JsonSerializerSettings js = new JsonSerializerSettings();             js.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;             return new AjaxResult(ifSuccess, JsonConvert.SerializeObject(resultPath, Formatting.Indented, js), msg);

        }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!