NPOI介紹:
NPOI是指構(gòu)建在POI 3.x版本之上的一個(gè)程序,NPOI可以在沒有安裝Office的情況下對(duì)Word或Excel文檔進(jìn)行讀寫操作。
NPOI是一個(gè)開源的C#讀寫Excel、WORD等微軟OLE2組件文檔的項(xiàng)目。
NPOI優(yōu)勢(shì):
1、您可以完全免費(fèi)使用該框架
2、包含了大部分EXCEL的特性(單元格樣式、數(shù)據(jù)格式、公式等等)
3、專業(yè)的技術(shù)支持服務(wù)(24*7全天候) (非免費(fèi))
4、支持處理的文件格式包括xls, xlsx, docx.
5、采用面向接口的設(shè)計(jì)架構(gòu)( 可以查看 NPOI.SS 的命名空間)
6、同時(shí)支持文件的導(dǎo)入和導(dǎo)出
7、基于.net 2.0 也支持xlsx 和 docx格式(當(dāng)然也支持.net 4.0)
8、來自全世界大量成功且真實(shí)的測(cè)試Cases
9、大量的實(shí)例代碼
11、你不需要在服務(wù)器上安裝微軟的Office,可以避免版權(quán)問題。
12、使用起來比Office PIA的API更加方便,更人性化。
13、你不用去花大力氣維護(hù)NPOI,NPOI Team會(huì)不斷更新、改善NPOI,絕對(duì)省成本。
14、不僅僅對(duì)與Excel可以進(jìn)行操作,對(duì)于doc、ppt文件也可以做對(duì)應(yīng)的操作
下面使用到NPOI.HSSF進(jìn)行實(shí)戰(zhàn)(Microsoft Excel BIFF(Excel 97-2003)格式讀寫庫(kù))
第一步首先是要引入NPOI依賴包了
第二步,準(zhǔn)備Excel模板
第三步編寫代碼
以下僅展示核心代碼文章來源:http://www.zghlxwxcb.cn/news/detail-402556.html
public void PrintExcle(HttpQueryData httpQueryData, List<PrintData> prints) {
string path= "Excel模板.xls";//模板路徑
Random random = new Random();
var localTemplatPath = TempFileAccess.TempDir + @"\" + httpQueryData.TaskNo + " " + prints[0].DeptName + random.Next(100000, 999999) + "Excel模板.xls";//新生成文件路徑以及文件名與格式
File.Copy(path , localTemplatPath);
using (var fs = File.OpenRead(localTemplatPath))
{
//此處應(yīng)該再判斷文件的格式是xls還是xlsx,如果是xlsx則使用XSSFWorkbook
//HSSFWorkbook:是操作Excel2003以前(包括2003)的版本、XSSFWorkbook:是操作Excel2007的版本
var workbook = new HSSFWorkbook(fs);
var sheet = workbook.GetSheetAt(0);
//基本信息(Row和Cell分別代表著行、列,計(jì)數(shù)都是從0開始,比如說Excel里面第一個(gè)格子就是0,0)
//此處代碼是根據(jù)自己的模板去寫GetRow與GetCell的值
var rowIndex = 1;
sheet.GetRow(rowIndex).GetCell(1).SetCellValue(httpQueryData.TaskNo);
if(!string.IsNullOrWhiteSpace(httpQueryData.InputDate))
sheet.GetRow(rowIndex).GetCell(4).SetCellValue(httpQueryData.InputDate.Split(' ')[0]);
sheet.GetRow(rowIndex).GetCell(7).SetCellValue(DateTime.Now.ToString("yyyy-MM-dd"));
rowIndex = 2;
sheet.GetRow(rowIndex).GetCell(1).SetCellValue(httpQueryData.DelegateOrgName);
sheet.GetRow(rowIndex).GetCell(4).SetCellValue(" ");
sheet.GetRow(rowIndex).GetCell(7).SetCellValue(httpQueryData.BusinessFromDisplay);
rowIndex = 3;
sheet.GetRow(rowIndex).GetCell(1).SetCellValue(" ");
sheet.GetRow(rowIndex).GetCell(4).SetCellValue(" ");
sheet.GetRow(rowIndex).GetCell(7).SetCellValue(prints[0].DeptName);
//****動(dòng)態(tài)列表信息,先加行,在填入信息
if (prints.Count > 0)
{
for (int i = 0; i < prints.Count - 1; i++)
{
sheet.CopyRow(5, 6 + i);
}
}
int count = 0;
rowIndex = 5;
for (int i = 0; i < prints.Count; i++) {
var curRow = sheet.GetRow(rowIndex);
var data = prints[i];
curRow.GetCell(0).SetCellValue(i+1);
curRow.GetCell(1).SetCellValue(data.Name);
curRow.GetCell(3).SetCellValue(data.FactoryNo);
curRow.GetCell(4).SetCellValue(data.Count);
curRow.GetCell(5).SetCellValue(data.InspectionAttribute);
curRow.GetCell(7).SetCellValue(data.CertificateRequirements);
curRow.GetCell(9).SetCellValue(data.Remark);
count = count + int.Parse(data.Count);
rowIndex = rowIndex + 1;
}
sheet.GetRow(rowIndex).GetCell(4).SetCellValue(count);
//保存
using (FileStream localFs = File.OpenWrite(localTemplatPath))
{
workbook.Write(localFs);
localFs.Close();
}
}
System.Diagnostics.Process.Start(localTemplatPath);
}
最終效果文章來源地址http://www.zghlxwxcb.cn/news/detail-402556.html
到了這里,關(guān)于C#讀寫導(dǎo)入導(dǎo)出Excel表格模板(NPOI)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!