使用npoi匯出,讀取excel,具有可追加功能
看**
1view codeusing
system;
2using
system.collections.generic;
3using
system.text;
4using
system.io;
5using
system.data;
6using
npoi.ss.usermodel;
7using
npoi.xssf.usermodel;
8using
npoi.hssf.usermodel;910
namespace
npoiexcel
1140
else
4147
48try
4956
else
5760}61
else
6265
6667
75 count = 1;76
}77else
7881}82
83 count = sheet.lastrownum + 1;84
85for (i = 0; i < data.rows.count; ++i)
8692 ++count;93}
9495}96
catch
(exception ex)
97101
}102
103 filestream outfs = new
filestream(filename, filemode.open);
104workbook.write(outfs);
105outfs.close();
106return
count;
107}
108109
///110
///將excel中的資料匯入到datatable中
111///
112///
excel工作薄sheet的名稱
113///
第一行是否是datatable的列名
114///
返回的datatable
115public
static datatable exceltodatatable(string filename, string sheetname = null, bool isfirstrowcolumn = true
)116
136}
137else
138141
if (sheet != null
)142
159}
160}
161 startrow = sheet.firstrownum + 1
;162
}163
else
164167
168//
最後一列的標號
169int rowcount =sheet.lastrownum;
170for (int i = startrow; i <= rowcount; ++i)
171181
data.rows.add(datarow);
182}
183}
184185
return
data;
186}
187catch
(exception ex)
188192
}193
194195
}196 }
看測試
1view codeusing
system;
2using
system.collections.generic;
3using
system.linq;
4using
system.text;
5using
system.data;
6using
npoiexcel;78
namespace
test925
dt.rows.add(dr);26}
27 excelex.datatabletoexcel("
d:\\123.xlsx
",dt);
28dt.rows.clear();
2930
for (int i = 0; i < 50; i++)
3137
dt.rows.add(dr);38}
3940 excelex.datatabletoexcel("
d:\\123.xlsx
", dt,true
);41
console.readkey();42}
43}44 }
看結果
C 使用NPOI 匯出Excel
npoi可以在沒有安裝office的情況下對word或excel文件進行讀寫操作 下面介紹下npoi操作excel的方法 這裡使用的是net4.0 將下面幾個dll新增到專案中並引用 廢話不多說 上 create 2016 11 30 by sly 要匯入的資料 datatable的列名是否要匯入 ...
使用NPOI匯出Excel檔案
使用npoi匯出excel檔案,本例項使用了asp.net mvc。1 使用npoi匯出excel檔案 例項 匯出商品列表。要求 1 通過npoi匯出匯出商品列表資訊 2 使用excel函式計算商品總金額 在controllers控制器目錄中建立exportcontroller.cs控制器 usin...
使用NPOI匯出Excel檔案
using comm.library.xml using npoi.hssf.usermodel using npoi.hssf.util using npoi.ss.usermodel using npoi.xssf.usermodel using system using system.coll...