using NPOI.SS.UserModel;
|
using NPOI.XSSF.UserModel;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using Microsoft.Office.Interop.Excel;
|
|
namespace BatchService.Framework.Utility
|
{
|
/// <summary>
|
/// Exl坐标
|
/// </summary>
|
public class TableValue
|
{
|
//行号
|
public int GetRow { get; set; }
|
//列号
|
public int GetCell { get; set; }
|
|
//坐标值
|
public string GetValue { get; set; }
|
|
}
|
|
public class NPOIExcel
|
{
|
/// <summary>
|
/// 修改excel数据
|
/// </summary>
|
/// <param name="outputFile">excel路径</param>
|
/// <param name="sheetname">excel表单</param>
|
/// <param name="list">修改集合</param>
|
public static void UpdeteExcel(string outputFile, string sheetname, List<TableValue> list)
|
{
|
LogHelper.Debug("UpdeteExcel修改方法");
|
try
|
{
|
FileStream readfile = new FileStream(outputFile, FileMode.Open, FileAccess.Read);
|
XSSFWorkbook hssfworkbook = new XSSFWorkbook(readfile);
|
ISheet sheet1 = hssfworkbook.GetSheet(sheetname);
|
for (int i = 0; i < list.Count; i++)
|
{
|
try
|
{
|
sheet1.GetRow(list[i].GetRow).GetCell(list[i].GetCell).SetCellValue(list[i].GetValue);
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Debug(ex.Message);
|
// wl.WriteLogs(ex.ToString());
|
throw;
|
}
|
}
|
try
|
{
|
readfile.Close();
|
FileStream writefile = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
|
hssfworkbook.Write(writefile);
|
writefile.Close();
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Debug(ex.Message);
|
// wl.WriteLogs(ex.ToString());
|
}
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Debug("初始化失败");
|
LogHelper.Debug(ex.Message.ToString());
|
}
|
|
|
}
|
/// <summary>
|
/// 返回修改后的Exl工作表
|
/// </summary>
|
/// <param name="TempletFileName">摸版路径</param>
|
/// <param name="TableValues">修改坐标值</param>
|
/// <returns>返回修改后的工作表</returns>
|
public static XSSFWorkbook GetExcelValue(string TempletFileName, string Sheetnub, List<TableValue> TableValues)
|
{
|
//返回工作表
|
XSSFWorkbook hssfworkbook=null;
|
//打开摸版文件
|
using (FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read))
|
{
|
hssfworkbook = new XSSFWorkbook(file);
|
file.Close();
|
}
|
//获取摸版表单的对象
|
XSSFSheet ws = (XSSFSheet)hssfworkbook.GetSheet(Sheetnub);
|
//赋值表单
|
if (TableValues != null && TableValues.Count > 0)
|
{
|
int x = 0;
|
TableValues.ForEach(Call =>
|
{
|
x += 1;
|
if (Call.GetValue==null)
|
{
|
ws.GetRow(Call.GetRow).GetCell(Call.GetCell).SetCellValue("");
|
}
|
else
|
{
|
ws.GetRow(Call.GetRow).GetCell(Call.GetCell).SetCellValue(Call.GetValue);
|
}
|
|
|
});
|
}
|
return hssfworkbook;
|
}
|
}
|
}
|