///
/// excel小幫手
///
public
static
class
excelhelper;if
(ofd.
showdialog()
== dialogresult.ok)
else
}iworkbook workbook =
null
;using
var fs = file.
openread
(filename);if
(path.
getextension
(filename)
==".xls"
)else
if(path.
getextension
(filename)
==".xlsx")if
(workbook ==
null)if
(!string
.isnullorempty
(sheetname))"
);}return
sheettodatatable
(sheet);}
else")
;}return
sheettodatatable
(sheet);}
}///
/// 讀取多表型excel轉化為dataset
///
/// excel檔案路徑
///
public
static
dataset
exceltodataset
(string filename =
null);
if(ofd.
showdialog()
== dialogresult.ok)
else
}iworkbook workbook =
null
;using
var fs = file.
openread
(filename);if
(path.
getextension
(filename)
==".xls"
)else
if(path.
getextension
(filename)
==".xlsx")if
(workbook ==
null
)dataset ds =
newdataset()
;for
(int i =
0; i < workbook.numberofsheets; i++
)return ds;
}///
/// datable匯出成excel
///
/// 需要匯出的datatable
/// excel檔案路徑
/// 是否顯示匯出完成對話方塊
public
static
void
datatabletoexcel
(datatable dt =
null
,string filename =
null
,bool isshowresult =
true);
if(sfd.
showdialog()
== dialogresult.ok)
else}if
(dt ==
null
)iworkbook workbook =
newxssfworkbook()
;string fileext = path.
getextension
(filename);if
(fileext ==
".xls"
)isheet sheet =
string
.isnullorempty
(dt.tablename)
? workbook.
createsheet
("sheet1"
): workbook.
createsheet
(dt.tablename)
;datatabletosheet
(ref sheet, dt)
;using
var stream =
newmemorystream()
; workbook.
write
(stream)
;var buf = stream.
toarray()
;//儲存為excel檔案
using
filestream fs =
newfilestream
(filename, filemode.create, fileaccess.write)
; fs.
write
(buf,
0, buf.length)
; fs.
flush()
;if(isshowresult)
}///
/// 將dataset匯出成多sheet型excel
///
///
/// excel檔案路徑
/// 是否顯示匯出完成對話方塊
public
static
void
datasettoexcel
(dataset ds =
null
,string filename =
null
,bool isshowresult =
true);
if(sfd.
showdialog()
== dialogresult.ok)
else}if
(ds ==
null
)iworkbook workbook =
newxssfworkbook()
;string fileext = path.
getextension
(filename);if
(fileext ==
".xls"
)foreach
(datatable dt in ds.tables)
using
var stream =
newmemorystream()
; workbook.
write
(stream)
;var buf = stream.
toarray()
;//儲存為excel檔案
using
filestream fs =
newfilestream
(filename, filemode.create, fileaccess.write)
; fs.
write
(buf,
0, buf.length)
; fs.
flush()
;if(isshowresult)
}///
/// 獲取單元格型別
///
///
///
internal
static
object
getvaluetype
(icell cell)
switch
(cell.celltype)
else
case celltype.string:
//string:
return cell.stringcellvalue;
case celltype.error:
//error:
return cell.errorcellvalue;
case celltype.formula:
//formula:
default
:return
"="+ cell.cellformula;}}
///
/// 將excel的isheet轉化為datatable
///
/// npoi中的乙個isheet
///
internal
static
datatable
sheettodatatable
(isheet sheet)")
;}else
}// 新增行資料
for(
int i =
1; i <= sheet.lastrownum; i++
)var dr = dt.
newrow()
;for
(int j =
0; j < row.lastcellnum; j++
) dt.rows.
add(dr);}
return dt;
}///
/// 將datatable資料儲存至npoi中的乙個isheet
///
///
internal
static
void
datatabletosheet
(ref
isheet sheet,
datatable dt)
//資料
for(
int i =
0; i < dt.rows.count; i++
)else}}
}}}
C 使用NPOI 匯出Excel
npoi可以在沒有安裝office的情況下對word或excel文件進行讀寫操作 下面介紹下npoi操作excel的方法 這裡使用的是net4.0 將下面幾個dll新增到專案中並引用 廢話不多說 上 create 2016 11 30 by sly 要匯入的資料 datatable的列名是否要匯入 ...
C 使用NPOI匯出Excel
當記錄數超出65536時,有兩種方式處理 一是呼叫writetodownload65536方法建立多個excel。二是呼叫writetodownload方法 在同乙個excel中建多個sheet。若在同一excel中建多個sheet,若記錄數達數十萬,會導致位元組流溢位的問題,解決辦法是先獲取mem...
C 使用NPOI的Excel匯出
前言 開發環境 vs2017 sqlserver 谷歌瀏覽器 正文1.在專案中引用npoi a.在neget裡搜尋npoi直接安裝 推薦 2.建立個類,內容如下 public byte createexcel datatable datatable,string name 輸入資料行 for int...