62,067
社区成员
发帖
与我相关
我的任务
分享
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Data;
using System.IO;
namespace EastWestWalk.User.Service.Logic.VipDomain
{
/// <summary>
/// 操作EXCEL
/// </summary>
public class ExcelHelper
{
private IWorkbook workbook = null;
private Stream steam = null;
private bool disposed;
/// <summary>
/// 函数
/// </summary>
public ExcelHelper()
{
disposed = false;
}
/// <summary>
/// Excel字节转DataTable
/// </summary>
/// <param name="StreamBytes">字节</param>
/// <param name="fileName">文件名</param>
/// <returns>结果</returns>
public DataTable ExcelToDataTable(byte[] StreamBytes, string fileName)
{
ISheet sheet = null;
DataTable data = new DataTable();
string sheetName = "Sheet1";
int startRow = 0;
try
{
steam = new MemoryStream(StreamBytes);
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
workbook = new XSSFWorkbook(steam);
else if (fileName.IndexOf(".xls") > 0) // 2003版本
workbook = new HSSFWorkbook(steam);
if (sheetName != null)
{
sheet = workbook.GetSheet(sheetName);
if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
{
sheet = workbook.GetSheetAt(0);
}
}
else
{
sheet = workbook.GetSheetAt(0);
}
if (sheet != null)
{
IRow firstRow = sheet.GetRow(0);
int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
ICell cell = firstRow.GetCell(i);
if (cell != null)
{
string cellValue = cell.StringCellValue;
if (cellValue != null)
{
DataColumn column = new DataColumn(cellValue);
data.Columns.Add(column);
}
}
}
startRow = sheet.FirstRowNum + 1;
// startRow = sheet.FirstRowNum;
//最后一列的标号
int rowCount = sheet.LastRowNum;
for (int i = startRow; i <= rowCount; ++i)
{
IRow row = sheet.GetRow(i);
if (row == null) continue; //没有数据的行默认是null
DataRow dataRow = data.NewRow();
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
dataRow[j] = row.GetCell(j).ToString();
}
data.Rows.Add(dataRow);
}
}
return data;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
return null;
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 释放资源
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (steam != null)
steam.Close();
}
steam = null;
disposed = true;
}
}
}
}
public ActionResult imputData(HttpPostedFileBase file) {
//获取上传的文件流
var stream = file.InputStream;
//通过第三方类库打开xls文件
//创建工作簿对象
var xBook = new Aspose.Cells.Workbook(stream);
//获取工作表对象
var xSheet = xBook.Worksheets[0];
//读取工作表内容
var val = xSheet.Cells[0, 0].Value;
//向工作表写入内容
xSheet.Cells[0, 0].PutValue("123"); //设置第一列第一行=123
//遍历获取值
for (int c = 0; c < xSheet.Cells.MaxColumn; c++) {
for (int r = 0; r < xSheet.Cells.MaxRow; r++) {
var data = xSheet.Cells[c, r].Value;
}
}
return Json(true);
}
核心是引用aspose.cells控件,或者引用office.excel也可以,不过调用office.excel需要服务器安装office,而aspose.cells只是一个类库,引用就可以了。