//datatable匯出excel
private
static
void gridtoexcelbynpoi(datatable dt, string
strexcelfilename)
icellstyle cellstyle =workbook.createcellstyle();
//為避免日期格式被excel自動替換,所以設定 format 為 『@』 表示一率當成text來看
cellstyle.dataformat = hssfdataformat.getbuiltinformat("@"
); cellstyle.borderbottom =npoi.ss.usermodel.borderstyle.thin;
cellstyle.borderleft =npoi.ss.usermodel.borderstyle.thin;
cellstyle.borderright =npoi.ss.usermodel.borderstyle.thin;
cellstyle.bordertop =npoi.ss.usermodel.borderstyle.thin;
npoi.ss.usermodel.ifont cellfont =workbook.createfont();
cellfont.boldweight = (short
)fontboldweight.normal;
cellstyle.setfont(cellfont);
//建立內容行
int irowindex = 1
;
int icellindex = 0
;
foreach (datarow rowitem in
dt.rows)
icellindex = 0
; irowindex++;
}//自適應列寬度
for (int i = 0; i < icolindex; i++)
//寫excel
filestream file = new
filestream(strexcelfilename, filemode.openorcreate);
workbook.write(file);
file.flush();
file.close();
messagebox.show(m_common_resourcemanager.getstring(
"export_to_excel_successfully
"), m_common_resourcemanager.getstring("
information
"), messageboxbuttons.ok, messageboxicon.information);
}catch
(exception ex)
finally
}
//////excel檔案導成datatable
/// ///
excel檔案目錄位址
///datatable表名
///excel sheet index
///public
static datatable xlstodatatable(string strfilepath, string strtablename,int
isheetindex)
if (strextname.equals("
.xls
") || strextname.equals("
.xlsx"))
//寫入內容
system.collections.ienumerator rows =sheet.getrowenumerator();
while
(rows.movenext())
datarow dr =dt.newrow();
foreach (icell item in
row.cells)
else
break
;
case
celltype.string:
string str =item.stringcellvalue;
if (!string
.isnullorempty(str))
else
break
;
case
celltype.unknown:
case
celltype.blank:
default
: dr[item.columnindex] = string
.empty;
break
; }
break
;
case
celltype.numeric:
if(dateutil.iscelldateformatted(item))
else
break
;
case
celltype.string:
string strvalue =item.stringcellvalue;
if (string
.isnullorempty(strvalue))
else
break
;
case
celltype.unknown:
case
celltype.blank:
default
: dr[item.columnindex] = string
.empty;
break
; }
}dt.rows.add(dr);}}
}return
dt; }
npoi匯入匯出excel
1.匯入 將datatable資料匯入到excel中 要匯入的資料 datatable的列名是否要匯入 要匯入的excel的sheet的名稱 匯入資料行數 包含列名那一行 public int datatabletoexcel datatable data,string sheetname,bool...
NPOI實現 Excel匯入匯出
實現物件集合寫入到excel和excel資料讀出到物件集合的功能 自定義列屬性 public class columnattribute attribute public columnattribute string columnname 時間格式 public class datetimeform...
NPOI的匯入匯出EXCEL示範
using system using system.data using system.diagnostics using system.io using system.windows.forms using npoi.hssf.usermodel using npoi.ss.usermodel u...