中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久

asp.net:常見數據導入excel
來源:易賢網 閱讀:801 次 日期:2014-11-20 10:06:50
溫馨提示:易賢網小編為您整理了“asp.net:常見數據導入excel”,方便廣大網友查閱!

引言

項目中常用到將數據導入excel,將excel中的數據導入數據庫的功能,曾經也查找過相關的內容,將曾經用過的方案總結一下。

方案一

npoi

npoi 是 poi 項目的 .net 版本。poi是一個開源的java讀寫excel、word等微軟ole2組件文檔的項目。

使用 npoi 你就可以在沒有安裝 office 或者相應環境的機器上對 word/excel 文檔進行讀寫。npoi是構建在poi 3.x版本之上的,它可以在沒有安裝office的情況下對word/excel文檔進行讀寫操作。

優勢

(一)傳統操作excel遇到的問題:

1、如果是.net,需要在服務器端裝office,且及時更新它,以防漏洞,還需要設定權限允許.net訪問com+,如果在導出過程中出問題可能導致服務器宕機。

2、excel會把只包含數字的列進行類型轉換,本來是文本型的,excel會將其轉成數值型的,比如編號000123會變成123。

3、導出時,如果字段內容以“-”或“=”開頭,excel會把它當成公式進行,會報錯。

4、excel會根據excel文件前8行分析數據類型,如果正好你前8行某一列只是數字,那它會認為該列為數值型,自動將該列轉變成類似1.42702e+17格式,日期列變成包含日期和數字的。

(二)使用npoi的優勢

1、您可以完全免費使用該框架

2、包含了大部分excel的特性(單元格樣式、數據格式、公式等等)

3、專業的技術支持服務(24*7全天候) (非免費)

4、支持處理的文件格式包括xls, xlsx, docx.

5、采用面向接口的設計架構( 可以查看 npoi.ss 的命名空間)

6、同時支持文件的導入和導出

7、基于.net 2.0 也支持xlsx 和 docx格式(當然也支持.net 4.0)

8、來自全世界大量成功且真實的測試cases

9、大量的實例代碼

11、你不需要在服務器上安裝微軟的office,可以避免版權問題。

12、使用起來比office pia的api更加方便,更人性化。

13、你不用去花大力氣維護npoi,npoi team會不斷更新、改善npoi,絕對省成本。

npoi之所以強大,并不是因為它支持導出excel,而是因為它支持導入excel,并能“理解”ole2文檔結構,這也是其他一些excel讀寫庫比較弱的方面。通常,讀入并理解結構遠比導出來得復雜,因為導入你必須假設一切情況都是可能的,而生成你只要保證滿足你自己需求就可以了,如果把導入需求和生成需求比做兩個集合,那么生成需求通常都是導入需求的子集,這一規律不僅體現在excel讀寫庫中,也體現在pdf讀寫庫中,目前市面上大部分的pdf庫僅支持生成,不支持導入。

構成

npoi 1.2.x主要由poifs、ddf、hpsf、hssf、ss、util六部分組成。

npoi.poifs

ole2/activex文檔屬性讀寫庫

npoi.ddf

microsoft office drawing讀寫庫

npoi.hpsf

ole2/activex文檔讀寫庫

npoi.hssf

microsoft excel biff(excel 97-2003)格式讀寫庫

npoi.ss

excel公用接口及excel公式計算引擎

npoi.util

基礎類庫,提供了很多實用功能,可用于其他讀寫文件格式項目的開發

npoi組成部分

npoi 1.x的最新版為npoi 1.2.5,其中包括了以下功能:

1、讀寫ole2文檔

2、讀寫docummentsummaryinformation和summaryinformation

3、基于littleendian的字節讀寫

4、讀寫excel biff格式

5、識別并讀寫excel biff中的常見record,如rowrecord, stylerecord, extendedformatrecord

6、支持設置單元格的高、寬、樣式等

7、支持調用部分excel內建函數,比如說sum, countif以及計算符號

8、支持在生成的xls內嵌入打印設置,比如說橫向/縱向打印、縮放、使用的紙張等。

npoi 2.0主要由ss, hpsf, ddf, hssf, xwpf, xssf, openxml4net, openxmlformats組成,具體列表如下:

assembly名稱 模塊/命名空間 說明

npoi.dll

npoi.poifs

ole2/activex文檔屬性讀寫庫

npoi.dll

npoi.ddf

微軟office drawing讀寫庫

npoi.dll

npoi.hpsf

ole2/activex文檔讀寫庫

npoi.dll

npoi.hssf

微軟excel biff(excel 97-2003, doc)格式讀寫庫

npoi.dll

npoi.ss

excel公用接口及excel公式計算引擎

npoi.dll

npoi.util

基礎類庫,提供了很多實用功能,可用于其他讀寫文件格式項目的開發

npoi.ooxml.dll npoi.xssf excel 2007(xlsx)格式讀寫庫

npoi.ooxml.dll npoi.xwpf word 2007(docx)格式讀寫庫

npoi.openxml4net.dll npoi.openxml4net openxml底層zip包讀寫庫

npoi.openxmlformats.dll npoi.openxmlformats 微軟office openxml對象關系庫

(以上內容來自百度百科)從上表可知npoi組件已支持excel2007,記得之前用的時候只支持excel2003。很久沒研究過這玩意兒了。

案例

官網地址:http://npoi.codeplex.com/,可以從官網下載npoi2.x版本的。

首先引入

icsharpcode.sharpziplib.dll

npoi.dll

npoi.ooxml.dll

npoi.openxml4net.dll

npoi.openxmlformats.dll

然后引入命名空間:

using npoi.xssf.usermodel;

using npoi.ss.usermodel;

using npoi.hssf.usermodel;

輔助類

using npoi.xssf.usermodel;

using npoi.ss.usermodel;

using npoi.hssf.usermodel;

using system;

using system.collections.generic;

using system.data;

using system.io;

using system.linq;

using system.text;

using system.threading.tasks;

using npoi.ss.formula.eval;

namespace wolfy.common

{

///

/// 使用npoi組件

/// 需引入icsharpcode.sharpziplib.dll/npoi.dll/npoi.ooxml.dll/npoi.openxml4net.dll/npoi.openxmlformats.dll

/// office2007

///

public class npoiexcelhelper

{

///

/// 將excel文件中的數據讀出到datatable中

///

///

///

public static datatable excel2datatable(string file, string sheetname, string tablename)

{

datatable dt = new datatable();

iworkbook workbook = null;

using (filestream fs = new filestream(file, filemode.open, fileaccess.read))

{

//office2003 hssfworkbook

workbook = new xssfworkbook(fs);

}

isheet sheet = workbook.getsheet(sheetname);

dt = export2datatable(sheet, 0, true);

return dt;

}

///

/// 將指定sheet中的數據導入到datatable中

///

/// 指定需要導出的sheet

/// 列頭所在的行號,-1沒有列頭

///

///

private static datatable export2datatable(isheet sheet, int headerrowindex, bool needheader)

{

datatable dt = new datatable();

xssfrow headerrow = null;

int cellcount;

try

{

if (headerrowindex < 0 || !needheader)

{

headerrow = sheet.getrow(0) as xssfrow;

cellcount = headerrow.lastcellnum;

for (int i = headerrow.firstcellnum; i <= cellcount; i++)

{

datacolumn column = new datacolumn(convert.tostring(i));

dt.columns.add(column);

}

}

else

{

headerrow = sheet.getrow(headerrowindex) as xssfrow;

cellcount = headerrow.lastcellnum;

for (int i = headerrow.firstcellnum; i <= cellcount; i++)

{

icell cell = headerrow.getcell(i);

if (cell == null)

{

break;//到最后 跳出循環

}

else

{

datacolumn column = new datacolumn(headerrow.getcell(i).tostring());

dt.columns.add(column);

}

}

}

int rowcount = sheet.lastrownum;

for (int i = headerrowindex + 1; i <= sheet.lastrownum; i++)

{

xssfrow row = null;

if (sheet.getrow(i) == null)

{

row = sheet.createrow(i) as xssfrow;

}

else

{

row = sheet.getrow(i) as xssfrow;

}

datarow dtrow = dt.newrow();

for (int j = row.firstcellnum; j <= cellcount; j++)

{

if (row.getcell(j) != null)

{

switch (row.getcell(j).celltype)

{

case celltype.boolean:

dtrow[j] = convert.tostring(row.getcell(j).booleancellvalue);

break;

case celltype.error:

dtrow[j] = erroreval.gettext(row.getcell(j).errorcellvalue);

break;

case celltype.formula:

switch (row.getcell(j).cachedformularesulttype)

{

case celltype.boolean:

dtrow[j] = convert.tostring(row.getcell(j).booleancellvalue);

break;

case celltype.error:

dtrow[j] = erroreval.gettext(row.getcell(j).errorcellvalue);

break;

case celltype.numeric:

dtrow[j] = convert.tostring(row.getcell(j).numericcellvalue);

break;

case celltype.string:

string strformula = row.getcell(j).stringcellvalue;

if (strformula != null && strformula.length > 0)

{

dtrow[j] = strformula.tostring();

}

else

{

dtrow[j] = null;

}

break;

default:

dtrow[j] = ;

break;

}

break;

case celltype.numeric:

if (dateutil.iscelldateformatted(row.getcell(j)))

{

dtrow[j] = datetime.fromoadate(row.getcell(j).numericcellvalue);

}

else

{

dtrow[j] = convert.todouble(row.getcell(j).numericcellvalue);

}

break;

case celltype.string:

string str = row.getcell(j).stringcellvalue;

if (!string.isnullorempty(str))

{

dtrow[j] = convert.tostring(str);

}

else

{

dtrow[j] = null;

}

break;

default:

dtrow[j] = ;

break;

}

}

}

dt.rows.add(dtrow);

}

}

catch (exception)

{

return null;

}

return dt;

}

///

/// 將datatable中的數據導入excel文件中

///

///

///

public static void datatable2excel(datatable dt, string file, string sheetname)

{

iworkbook workbook = new xssfworkbook();

isheet sheet = workbook.createsheet(sheetname);

irow header = sheet.createrow(0);

for (int i = 0; i < dt.columns.count; i++)

{

icell cell = header.createcell(i);

cell.setcellvalue(dt.columns[i].columnname);

}

//數據

for (int i = 0; i < dt.rows.count; i++)

{

irow row = sheet.createrow(i + 1);

for (int j = 0; j < dt.columns.count; j++)

{

icell cell = row.createcell(j);

cell.setcellvalue(dt.rows[i][j].tostring());

}

}

memorystream stream = new memorystream();

workbook.write(stream);

byte[] buffer = stream.toarray();

using (filestream fs = new filestream(file, filemode.create, fileaccess.write))

{

fs.write(buffer, 0, buffer.length);

fs.flush();

}

}

///

/// 獲取單元格類型

///

///

///

private static object getvaluetype(xssfcell cell)

{

if (cell == null)

{

return null;

}

switch (cell.celltype)

{

case celltype.blank:

return null;

case celltype.boolean:

return cell.booleancellvalue;

case celltype.error:

return cell.errorcellvalue;

case celltype.numeric:

return cell.numericcellvalue;

case celltype.string:

return cell.stringcellvalue;

case celltype.formula:

default:

return = + cell.stringcellvalue;

}

}

}

}

更多信息請查看IT技術專欄

更多信息請查看網絡編程
易賢網手機網站地址:asp.net:常見數據導入excel
由于各方面情況的不斷調整與變化,易賢網提供的所有考試信息和咨詢回復僅供參考,敬請考生以權威部門公布的正式信息和咨詢為準!

2026上岸·考公考編培訓報班

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
云南網警備案專用圖標
聯系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網
云南網警報警專用圖標
中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久
在线播放国产一区中文字幕剧情欧美 | 久久综合国产精品| 欧美伊人久久久久久久久影院| 亚洲欧美一区二区激情| 久久青草久久| 国产日本亚洲高清| av成人免费| 欧美成人免费播放| 国产资源精品在线观看| 亚洲尤物精选| 欧美国产精品一区| 依依成人综合视频| 亚洲专区在线| 欧美日韩亚洲三区| 亚洲欧洲一区二区三区| 久久av一区二区| 欧美性猛交99久久久久99按摩| 国产一区在线观看视频| 亚洲综合电影| 欧美日韩国产精品一区二区亚洲| 亚洲国产电影| 久久综合久久久| 激情五月婷婷综合| 欧美在线视频导航| 国产精品久久一区主播| 亚洲系列中文字幕| 欧美午夜久久久| 99精品国产在热久久| 欧美精品亚洲| 99天天综合性| 欧美视频四区| 亚洲欧美在线免费| 国产精品日韩在线播放| 亚洲欧美精品suv| 国产伦精品一区二区三区在线观看 | 日韩视频不卡| 欧美高清在线视频观看不卡| 在线日本成人| 欧美va天堂va视频va在线| 亚洲经典自拍| 欧美日韩国产精品一区二区亚洲| 亚洲精品欧美激情| 欧美日韩国产综合视频在线观看| 99在线观看免费视频精品观看| 欧美极品aⅴ影院| 一本大道av伊人久久综合| 久久久久九九九九| 激情综合五月天| 欧美精品高清视频| 亚洲图中文字幕| 国产亚洲成av人在线观看导航| 欧美一区二区三区久久精品茉莉花 | 亚洲精品国产视频| 国内久久婷婷综合| 玖玖视频精品| 9色porny自拍视频一区二区| 欧美性视频网站| 久久国产精品99国产| 韩日在线一区| 欧美日韩视频在线一区二区| 亚洲一区二区三区在线视频| 国语自产偷拍精品视频偷| 免费国产一区二区| 亚洲午夜未删减在线观看| 国产亚洲福利一区| 欧美国产欧美综合 | 亚洲另类在线一区| 国产精品一区久久久| 久久夜色精品国产欧美乱极品| 日韩午夜激情| 国内精品久久久| 欧美三级免费| 久久综合九色综合欧美狠狠| 99精品国产福利在线观看免费| 国产色综合天天综合网| 欧美日本国产一区| 久久久久一区二区三区| 亚洲一区二区三区欧美 | 国产精品国码视频| 久久综合99re88久久爱| 亚洲在线不卡| 夜夜狂射影院欧美极品| 在线播放不卡| 韩日在线一区| 国产一区二区三区四区hd| 国产精品激情电影| 欧美日韩国内自拍| 欧美激情中文字幕乱码免费| 久久综合综合久久综合| 久久久亚洲国产美女国产盗摄| 亚洲综合久久久久| 亚洲一区二区三区高清| 亚洲作爱视频| 一级日韩一区在线观看| 99国产精品自拍| 亚洲免费播放| 一区二区三区产品免费精品久久75| 亚洲国产高清一区| 性8sex亚洲区入口| 一区二区三区 在线观看视频| 在线观看亚洲精品| 尤物九九久久国产精品的特点 | 午夜精品久久久久影视| 亚洲视频在线播放| 亚洲经典自拍| 亚洲第一在线| 最新成人av网站| 精品成人a区在线观看| 国产在线观看精品一区二区三区| 国产精品免费网站在线观看| 欧美性大战xxxxx久久久| 欧美日韩在线播放一区| 欧美日韩视频在线一区二区观看视频 | 国产欧美一区二区三区久久| 国产精品国产一区二区| 国产精品男人爽免费视频1| 国产精品美女主播| 国产日本欧美一区二区三区在线| 国产精品夜夜嗨| 国产一区二区三区在线观看免费视频| 国产日产欧产精品推荐色 | 在线亚洲欧美视频| 亚洲天堂网站在线观看视频| 亚洲欧美日韩国产成人精品影院| 欧美亚洲三区| 免费视频一区二区三区在线观看| 欧美精品午夜| 国产日韩欧美一区二区| 伊人婷婷欧美激情| 日韩亚洲欧美精品| 欧美亚洲在线视频| 免费在线日韩av| 国产精品美女黄网| 亚洲第一成人在线| 亚洲久久视频| 欧美在线免费| 欧美精品一区二区三区久久久竹菊 | 久久久欧美精品| 欧美国产先锋| 国产麻豆91精品| 亚洲高清在线| 在线亚洲观看| 免费欧美视频| 国产精品一卡| 亚洲精品视频在线观看免费| 午夜精品www| 欧美精品123区| 韩日视频一区| 亚洲专区一区| 欧美日韩国产一区精品一区| 国产一区二区看久久| 一区二区三区四区五区精品| 久久手机免费观看| 国产精品推荐精品| 日韩视频免费| 欧美成人精品一区二区| 韩国在线一区| 欧美专区在线播放| 国产精品电影在线观看| 亚洲国产欧洲综合997久久| 午夜视频久久久| 国产精品s色| 99精品视频免费| 欧美激情第三页| 激情综合色丁香一区二区| 亚洲尤物精选| 欧美午夜一区二区| 日韩视频亚洲视频| 欧美成人免费一级人片100| 黄色成人在线网站| 欧美在线视频免费观看| 国产精品欧美一区二区三区奶水| 亚洲区国产区| 欧美精品在欧美一区二区少妇| 在线观看亚洲精品| 久久综合九色99| 亚洲激情在线激情| 欧美激情一区二区三区蜜桃视频| 国产一区二区剧情av在线| 午夜精品电影| 国产一区二区三区高清播放| 久久av老司机精品网站导航| 国产日韩精品入口| 久久男人资源视频| 精品不卡视频| 欧美成人自拍视频| 日韩午夜电影av| 国产精品大片wwwwww| 亚洲欧美日韩一区| 国产一区在线看| 欧美成人精品在线| 一本久久综合亚洲鲁鲁| 国产精品高清网站| 欧美一区二区精品久久911| 国模叶桐国产精品一区| 欧美激情aⅴ一区二区三区| 日韩视频一区二区在线观看 | 999亚洲国产精| 国产精品白丝av嫩草影院| 亚洲一区在线播放|