把資料匯出excel的應用很廣泛,如果使用依賴於excel的com方法,則難度就很大,而且還必須安裝excel,否則就不能匯出。使用npoi匯出excel很簡單,只需要新增乙個程式集npoi.dll的引用就可以,而且不依賴於excel,也就是不需要安裝excel。自然比依賴於excel的com方法好很多。
核心**
[csharp]view plain
copy
public
static
void exportbyweb(listlist, string strfilename)
//}// 設定編碼和附件格式
#region 新建表,填充表頭,填充列頭,樣式
if (rowindex == 65535 || rowindex == 0)
#region 表頭及樣式
addtitle(workbook, sheet, columnnames.count, "產品線**彙總");
#endregion
#region 列頭及樣式
addcolumntitle(workbook, sheet, columnnames, arrcolumnwidth);
#endregion
rowindex = 2;
} #endregion
writedatarows(list, workbook, columnnames.count, sheet, rowindex);
using (memorystream ms = new memorystream())
} ///
/// 新增第一行的標題
///
///
///
///
///
private
static
void addtitle(hssfworkbook workbook, hssfsheet sheet, int maxcolumncount, string strheadertext)
private
static
void addcolumntitle(hssfworkbook workbook, hssfsheet sheet, list columnnames, int arrcolumnwidth)
} private
static listwritedatarows(listparentlist, hssfworkbook workbook, int maxcolumncount, hssfsheet sheet, int rowindex)
else
count++;
newcell = (hssfcell)datarow.createcell(count);
newcell.cellstyle = style2;
newcell.setcelltype(celltype.numeric);
if (row.p_pricehigh.hasvalue)
else
count++;
newcell = (hssfcell)datarow.createcell(count);
newcell.cellstyle = style2;
newcell.setcelltype(celltype.numeric);
newcell.setcellvalue(row.total_price);
count++;
//}#endregion
rowindex++;
} return listnext;
}
匯出結果
更有通用性的**
[csharp]view plain
copy
public
static
class exporttofile
} }
///
/// 用於web匯出
///
/// 源datatable
/// 表頭文字
/// 檔名
/// 柳永法 2010-5-8 22:21:41
public
static
void exportbyweb(listlist, string strheadertext, string strfilename, dictionary fieldnames)
private
static memorystream export(listlist, string strheadertext, dictionary fieldnames)
int rowindex = 0;
foreach (tmodel row in list)
#region 表頭及樣式
addfirstrow(workbook, sheet, fieldnames, strheadertext);
#endregion
#region 列頭及樣式
addsecondrow(workbook, sheet, fieldnames, arrcolwidth);
#endregion
rowindex = 2;
} #endregion
#region 填充內容
hssfrow datarow = (hssfrow)sheet.createrow(rowindex);
count = 0;
foreach (keyvaluepair column in fieldnames)
switch (dateytype)
count++;
} #endregion
rowindex++;
} using (memorystream ms = new memorystream())
}
private
static
void addfirstrow(hssfworkbook workbook, hssfsheet sheet, dictionary fieldnames, string strheadertext)
private
static
void addsecondrow(hssfworkbook workbook, hssfsheet sheet, dictionary fieldnames, int arrcolwidth)
} }
匯出固定的excel
string sp file.separator string templatepath corestaticmanager.getfileroot system sp template sp aaaas.xls string fullmyfilename corestaticmanager.get...
excel的匯入匯出
匯入 public function excel put private function excel fileput filepath,data,tablename 載入excel檔案 phpexcel phpreader load filepath 讀取excel檔案中的第乙個工作表 curre...
Excel的匯入匯出
匯入 php view plain copy public function excel put private function excel fileput filepath data tablename 載入excel檔案 phpexcel phpreader load filepath 讀取e...