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

}

NPOI 导入 导出的更多相关文章

  1. c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出

    c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...

  2. NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中

    以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...

  3. NPOI导入导出Excel

    .net mvc利用NPOI导入导出excel 注意:如何导出的提交方式ajax导出是失效的! 解决方案是:js处理l两个表单的提交  代码:  第一步. 在页面里面加入2个隐藏的iframe, 如下 ...

  4. .Net core NPOI导入导出Excel

    最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...

  5. c#.net 使用NPOI导入导出标准Excel (asp.net winform csharp)

    尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...

  6. 使用NPOI导入导出标准的Excel

    关于NPOI NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex. ...

  7. ASP.NET- 使用NPOI导入导出标准Excel

    尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...

  8. 使用NPOI导入导出标准Excel

    尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...

  9. NPOI导入导出Excel数据

    代码: using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; usi ...

  10. net core WebApi——使用NPOI导入导出操作

    目录 前言 NPOI 测试 小结 @ 前言 时间过得好快,在之前升级到3.0之后,就感觉好久没再动过啥东西了,之前有问到Swagger的中文汉化,虽说我觉得这种操作的意义不是太大,也是多少鼓捣了下,其 ...

随机推荐

  1. unity, 集成iOS广告sdk注意事项

    ----更新:2015-10-22 今天在unity里集成讯飞语音听写的iOS sdk,结果发现前面集成domob广告的方法搞复杂了. 其实,直接把UnityViewControllerBase当做s ...

  2. nginx configure参数

    下面是nginx源码程序的configure参数: --prefix= 指向安装目录.默认为:/usr/local/nginx --sbin-path= 指定执行程序文件存放位置.默认为:prefix ...

  3. ipk CONTROL 目录的作用

    CONTROL文件夹下的文件意义preinst        - shell script,在ipk包开始安装前执行;postinst       - shell script,在ipk包安装后执行; ...

  4. jquery中绑定点击事件

    $("body").on("click",".tab-contentBox td",function(){}; $(".tab-c ...

  5. 域名出售(www.shopbao.com)

    www.shopbao.com 前有淘宝,今有商宝. 商宝网站,精彩无限. 因经济原因,忍痛割爱,欲出售该商业域名. 有意者,请联系:18610310405 MAIL: jieisme@163.com

  6. 【转】【Mac系统】之ADB命令总结

    参考文章: 张明云:<Android ADB命令大全(通过ADB命令查看wifi密码.MAC地址.设备信息.操作文件.查看文件.日志信息.卸载.启动和安装APK等)> Yjnull:< ...

  7. Div+CSS布局入门教程

    http://www.blueidea.com/tech/site/2006/3574.asp ———————————————————————————————————————————————————— ...

  8. LOCAL_SHARED_LIBRARIES 与 LOCAL_LDLIBS,LOCAL_LDFLAGS的区别

    LOCAL_LDLIBS :链接的库不产生依赖关系,一般用于不需要重新编译的库,如库不存在,则会报错找不到.且貌似只能链接那些存在于系统目录下本模块需要连接的库.如果某一个库既有动态库又有静态库,那么 ...

  9. 推荐个WIN7下小巧的可转录声音的软件-Audio Record Wizard V6.99

    之前是XP上用的是 WaveCN 2.0.0.5,但这个软件好久没更新了,不支持WIN7 最终找到了Audio Record Wizard V6.99,尽管没 WaveCN 2.0.0.5好用,但也全 ...

  10. ftp uploadFileAction(重要)

    TelnetOUtputStream os = ftpClient.put(filename); File file_in = new File(localPath); FileInputStream ...