資源簡介
C#操作excel,導入excel,導出excel,對excel進行操作

代碼片段和文件信息
using?System.Collections.Generic;
using?System.Data;
using?System.IO;
using?System.Linq;
using?NPOI.HSSF.UserModel;
using?NPOI.SS.UserModel;
using?NPOI.XSSF.UserModel;
public?class?ExcelHelper
{
????public?class?x2003
????{
????????#region?Excel2003
????????///?
????????///?將Excel文件中的數據讀出到DataTable中(xls)
????????///?
????????///?
????????///?
????????public?static?DataTable?ExcelToTableForXLS(string?file)
????????{
????????????DataTable?dt?=?new?DataTable();
????????????using?(FileStream?fs?=?new?FileStream(file?FileMode.Open?FileAccess.Read))
????????????{
????????????????HSSFWorkbook?hssfworkbook?=?new?HSSFWorkbook(fs);
????????????????ISheet?sheet?=?hssfworkbook.GetSheetAt(0);
????????????????//表頭
????????????????IRow?header?=?sheet.GetRow(sheet.FirstRowNum);
????????????????List?columns?=?new?List();
????????????????for?(int?i?=?0;?i?????????????????{
????????????????????object?obj?=?GetValueTypeForXLS(header.GetCell(i)?as?HSSFCell);
????????????????????if?(obj?==?null?||?obj.ToString()?==?string.Empty)
????????????????????{
????????????????????????dt.Columns.Add(new?DataColumn(“Columns“?+?i.ToString()));
????????????????????????//continue;
????????????????????}
????????????????????else
????????????????????????dt.Columns.Add(new?DataColumn(obj.ToString()));
????????????????????columns.Add(i);
????????????????}
????????????????//數據
????????????????for?(int?i?=?sheet.FirstRowNum?+?1;?i?<=?sheet.LastRowNum;?i++)
????????????????{
????????????????????DataRow?dr?=?dt.NewRow();
????????????????????bool?hasValue?=?false;
????????????????????foreach?(int?j?in?columns)
????????????????????{
????????????????????????dr[j]?=?GetValueTypeForXLS(sheet.GetRow(i).GetCell(j)?as?HSSFCell);
????????????????????????if?(dr[j]?!=?null?&&?dr[j].ToString()?!=?string.Empty)
????????????????????????{
????????????????????????????hasValue?=?true;
????????????????????????}
????????????????????}
????????????????????if?(hasValue)
????????????????????{
????????????????????????dt.Rows.Add(dr);
????????????????????}
????????????????}
????????????}
????????????return?dt;
????????}
????????///?
????????///?將DataTable數據導出到Excel文件中(xls)
????????///?
????????///?
????????///?
????????public?static?void?TableToExcelForXLS(DataTable?dt?string?file)
????????{
????????????HSSFWorkbook?hssfworkbook?=?new?HSSFWorkbook();
????????????ISheet?sheet?=?hssfworkbook.CreateSheet(“Test“);
????????????//表頭
????????????IRow?row?=?sheet.CreateRow(0);
????????????for?(int?i?=?0;?i?????????????{
????????????????ICell?cell?=?row.CreateCell(i);
????????????????cell.SetCellValue(dt.Columns[i].ColumnName);
????????????}
????????????//數據
????????????for?(int?i?=?0;?i?????????????{
????????????????I
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-10-17?11:35??導出excel\
?????文件?????????632??2017-10-13?17:07??導出excel\ImportHelper.aspx
?????文件????????8675??2017-10-13?17:08??導出excel\ImportHelper.aspx.cs
?????文件????????1143??2017-10-13?17:07??導出excel\ImportHelper.aspx.designer.cs
?????目錄???????????0??2017-10-19?09:45??導入excel\
?????文件????????9663??2017-07-28?10:08??導入excel\ExcelHelper.cs
?????目錄???????????0??2017-10-18?15:53??導入excel\導入excelBll\
?????文件??????200704??2017-03-14?11:38??導入excel\導入excelBll\ICSharpCode.SharpZipLib.dll
?????文件?????1599488??2017-03-14?11:38??導入excel\導入excelBll\NPOI.dll
?????文件??????395776??2017-03-14?11:38??導入excel\導入excelBll\NPOI.OOxm
?????文件??????431466??2017-03-14?11:38??導入excel\導入excelBll\NPOI.OOxm
?????文件???????84480??2017-03-14?11:38??導入excel\導入excelBll\NPOI.Openxm
?????文件??????153211??2017-03-14?11:38??導入excel\導入excelBll\NPOI.Openxm
?????文件?????1866240??2017-03-14?11:38??導入excel\導入excelBll\NPOI.Openxm
?????文件?????2133648??2017-03-14?11:38??導入excel\導入excelBll\NPOI.xm
- 上一篇:net課設_網上購物系統
- 下一篇:winform支付寶demo
評論
共有 條評論