NPOI讀取Excel檔
来源:互联网 发布:易游网络验证通杀器 编辑:程序博客网 时间:2024/05/18 17:42
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace WebDB.Common.Utility
{
/// <summary>
/// 轉換輔助型別
/// </summary>
public static class ConvertUtility
{
#region 使用 NPOI 方式匯出Excel
/// <summary>
/// 轉換Excel資料至DataTable
/// </summary>
/// <param name="filePath">Excel檔案路徑</param>
/// <returns>轉換Excel資料至DataTable</returns>
public static DataTable ExcelToDataTableByNPOI(string filePath)
{
DataSet dsData = ExcelToDataSetByNPOI(filePath);
if (dsData.Tables.Count > 0)
{
return dsData.Tables[0];
}
return new DataTable();
}
/// <summary>
/// 轉換Excel資料至DataSet
/// </summary>
/// <param name="filePath">Excel檔案路徑</param>
/// <returns>轉換Excel資料至DataSet</returns>
public static DataSet ExcelToDataSetByNPOI(string filePath)
{
DataSet dsData = new DataSet();
if (File.Exists(filePath))
{
using (FileStream stream = new FileStream(filePath, FileMode.Open))
{
HSSFWorkbook workbook = new HSSFWorkbook(stream);
short dateFormat = workbook.CreateDataFormat().GetFormat("yyyy/MM/dd HH:mm:ss");
for (int idx = 0, count = workbook.NumberOfSheets; idx < count; idx++)
{
ISheet sheet = workbook.GetSheetAt(idx);
if (sheet != null)
{
DataTable table = new DataTable();
IRow headerRow = sheet.GetRow(0);
if (headerRow != null)
{
int cellCount = headerRow.LastCellNum;
for (int i = headerRow.FirstCellNum; i < cellCount; i++)
{
DataColumn column = new DataColumn(headerRow.GetCell(i).ToString());
table.Columns.Add(column);
}
for (int i = (sheet.FirstRowNum + 1), rowCount = sheet.LastRowNum; i <= rowCount; i++)
{
IRow row = sheet.GetRow(i);
DataRow dataRow = table.NewRow();
if (row != null)
{
for (int j = row.FirstCellNum; j < cellCount; j++)
{
ICell cell = row.GetCell(j);
if (cell != null)
{
dataRow[j] = ParseNPOICellValue(cell, dateFormat);
}
}
}
else
{
for (int j = 0; j < cellCount; j++)
{
dataRow[j] = "";
}
}
table.Rows.Add(dataRow);
}
dsData.Tables.Add(table);
sheet = null;
}
}
}
workbook = null;
}
}
return dsData;
}
/// <summary>
/// 依欄位資料型態轉換成適當字串
/// </summary>
/// <param name="cell">欄位</param>
/// <param name="dateFormat">日期格式</param>
/// <returns>欄位資料</returns>
private static string ParseNPOICellValue(ICell cell, short dateFormat)
{
string cellValue = cell.ToString();
if (!cellValue.IsEmpty())
{
if (cell.CellType == CellType.NUMERIC)
{
try
{
if (HSSFDateUtil.IsCellDateFormatted(cell))
{
cell.CellStyle.DataFormat = dateFormat;
return cell.DateCellValue.ToString("yyyy/MM/dd HH:mm:ss");
}
}
catch
{
Log.Log.Write(String.Format("解析欄位資料發生錯誤:{0}({1})",
cellValue,
cell.DateCellValue));
}
}
return cellValue;
}
return "";
}
#endregion
}
}
- NPOI讀取Excel檔
- NPOI 存取 Excel 檔案利器
- excel讀寫使用NPOI Library
- 调用存储过程取到数据通过NPOI存到Excel中
- NPOI导出Excel
- NPOI 进行EXCEL操作
- 用npoi,读写excel
- NPOI生成Excel
- 用NPOI操作Excel
- C# NPOI 操作excel
- 利用npoi导出excel
- NPOI导出Excel
- NPOI Excel导入导出
- NPOI读取Excel
- NPOI 读写excel
- 使用NPOI导出EXCEL
- NPOI读取excel数据
- NPOI操作EXCEL
- VC操作Excel
- c语言代码和总结
- XE5 FTPSERVER
- 图形数据库之---neo4j(非关系型数据库)
- 关于求婚的那些浪漫故事
- NPOI讀取Excel檔
- 关于 解决Spring @ResponseBody注解返回字符串IE提示下载
- TOJ 2676 ZOJ 3201 Tree of Tree / 树形DP
- iOS 屏幕亮度
- win7下安装svn客户端
- __asm__ __volatile__ GCC的内嵌汇编语法 AT&T汇编语言语法(一)
- linux- API
- How to obtain iMac full system report
- java object的wait和Thread的sleep方法区别