C#使用NPOI實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出功能
本文實(shí)例為大家分享了C#使用NPOI實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出的具體代碼,供大家參考,具體內(nèi)容如下
Excel導(dǎo)入
使用OpenFileDiolog控件和button結(jié)合,選擇文件導(dǎo)入,將路徑顯示在文本框
設(shè)置按鈕點(diǎn)擊事件,將文件路徑賦給textBox.Text
private void Department_SUM_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? OpenFileDialog open = new OpenFileDialog(); ? ? ? ? ? ? open.ShowDialog(); ? ? ? ? ? ? textBox1.Text = open.FileName; ? ? ? ? }
實(shí)現(xiàn)excel導(dǎo)入,通過textBox1.Text來獲取文件路徑
private void button_Excel_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ?? ? ? ? ? ? ? FileStream fs = null; ? ? ? ? ? ? IWorkbook workbook = null; ? ? ? ? ? ? ISheet sheet = null; ? ? ? ? ? ? IRow row = null; String txtpath = textBox1.Text; ? ? ? ? ? ?? ? ? ? ? ? ? fs = File.OpenRead(txtpath); ? ? ? ? ? ? workbook = new XSSFWorkbook(fs); ? ? ? ? ? ? if (workbook != null) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? sheet = workbook.GetSheetAt(0); //獲取excel表格的第一個sheet ? ? ? ? ? ? ? ? if (sheet != null) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ?? ?//行的LastRowNum是0~N-1 ? ? ? ? ? ? ? ? ?? ?//列的LastCellNum是1~N ? ? ? ? ? ? ? ? ? ? int rowCount = sheet.LastRowNum; ? ? ? ? ? ? ? ? ? ? if (rowCount > 0) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? IRow firstrow = sheet.GetRow(0); ? ? ? ? ? ? ? ? ? ? ? ? int cellCount = firstrow.LastCellNum; ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i <= rowCount - 1; i++) ? ? ? ? ? ? ? ? ? ? ? ? { //獲取行的第6和第7列數(shù)據(jù),如果cell類型是文本,則通過StringCellValue取值 //如果cell類型是數(shù)值,則通過NumericCellValue來取值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? row = sheet.GetRow(i + 1); ? ? ? ? ? ? ? ? ? ? ? ? ? ?row.Cells[5].NumericCellValue; ?? ??? ??? ??? ??? ??? ?row.Cells[6].StringCellValue; //可以將Cell的數(shù)據(jù)存放在list中,這里假設(shè)將兩列cell的數(shù)據(jù)存入list1,list2 ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? fs.Close(); ? ? ? ? ? ? ? ? ? ? } ?? ??? ?//實(shí)際存放DataTable的位置 ?? ??? ?//調(diào)用自定義方法,實(shí)現(xiàn)導(dǎo)出 ?? ??? ?Add_DataTable_To_Excel(txtpath, table, sheet_name); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }
要實(shí)現(xiàn)excel導(dǎo)出,先將程序中的excel存為DataTable格式
本段代碼存在于上面代碼“//實(shí)際存放DataTable的位置”位置
DataTable table = new DataTable(); ? ? ? ? ? ? DataRow dr; ? ? ? ? ? ? table.Columns.Add("列名1", System.Type.GetType("System.String")); ? ? ? ? ? ? table.Columns.Add("列名2", System.Type.GetType("System.Double")); ? ? ? ? ? ? for (int i = 0; i < list4.Count; i++) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? dr = table.NewRow(); ? ? ? ? ? ? ? ? dr["列名1"] = list1i]; ? ? ? ? ? ? ? ? dr["列名2"] = list2[i].ToString("0.0000"); //將存入的數(shù)據(jù)格式保存為保留四位小數(shù) ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? table.Rows.Add(dr); ? ? ? ? ? ? }
通過方法導(dǎo)出excel,傳參為文件路徑,DataTable,表名
通過獲取要導(dǎo)入數(shù)據(jù)的目標(biāo)excel的內(nèi)容,導(dǎo)入數(shù)據(jù),要將excel導(dǎo)出的方式
public bool Add_DataTable_To_Excel(string output_file_path, DataTable dt, string sheet_name) ? ? ? ? { FileStream fs = null; ? ? ? ? ? ? IWorkbook workbook = null; ? ? ? ? ? ? ISheet sheet = null; ? ? ? ? ? ? IRow row = null; XSSFWorkbook xssfworkbook = null; ? ? ? ? ? ? ?fs = new FileStream(output_file_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); ? ? ? ? ? ? xssfworkbook = new XSSFWorkbook(fs); ? ? ? ? ? ? sheet = xssfworkbook.GetSheet(sheet_name); //設(shè)置馬上要使用的Cell數(shù)據(jù)格式 ? ? ? ? ? ? IDataFormat dataformat = xssfworkbook.CreateDataFormat(); ? ? ? ? ? ? ICellStyle style0 = xssfworkbook.CreateCellStyle(); ? ? ? ? ? ? style0.DataFormat = dataformat.GetFormat("0.0000"); ? ? ? ? ? ? ICellStyle style1 = xssfworkbook.CreateCellStyle(); ? ? ? ? ? ? style1.DataFormat = dataformat.GetFormat("0.00%"); if (sheet != null) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? int rowCount = sheet.LastRowNum; ? ? ? ? ? ? ? ? if (rowCount > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? IRow firstrow = sheet.GetRow(0); ? ? ? ? ? ? ? ? ? ? int cellCount = firstrow.LastCellNum; for (int i = 0; i <= rowCount - 1; i++) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? row = sheet.GetRow(i + 1); ? ? ? ? ? ? ? ? ? ? ? ? //表中有行為空,將空的行影響消除 ? ? ? ? ? ? ? ? ? ? ? ? if (!"".Equals(row.Cells[code_index].StringCellValue)) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? row = sheet.GetRow(i + 1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int j = 0; j <= dt.Rows.Count - 1; j++) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (row.Cells[code_index].StringCellValue.Equals(dt.Rows[j][0])) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ?//遍歷將DataTable中的數(shù)據(jù)存入Cell的值 ? row.Cells[1].SetCellValue(Convert.ToDouble(dt.Rows[j][0].ToString())); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? row.Cells[1].CellStyle = style0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?row.Cells[2].SetCellValue(Convert.ToDouble(dt.Rows[j][1].ToString()) / Convert.ToDouble(dt.Rows[j][1].ToString())); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? row.Cells[2].CellStyle = style1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } } } //導(dǎo)出excel MemoryStream stream = new MemoryStream(); xssfworkbook.Write(stream); ? ?var buf = stream.ToArray(); ? ? using (FileStream fss = new FileStream(txtpath, FileMode.Create, FileAccess.Write)) ? ? ? ?//保存為Excel文件 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? fss.Write(buf, 0, buf.Length); ? ? ? ? ? ? ? ? fss.Flush(); ? ? ? ? ? ? } ? ? ? ? ? ? return true; }
基礎(chǔ)的Excel文件的導(dǎo)入導(dǎo)出功能到這里全部完成
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中使用XmlDocument類來創(chuàng)建和修改XML格式的數(shù)據(jù)文件
這篇文章主要介紹了C#中使用XmlDocument類來創(chuàng)建和修改XML格式的數(shù)據(jù)文件的方法,XmlDocument類被包含在.NET框架中,需要的朋友可以參考下2016-04-04深入理解C#索引器(一種支持參數(shù)的屬性)與屬性的對比
本篇文章是對C#索引器(一種支持參數(shù)的屬性)與屬性的對比進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06如何使用Dapper處理多個結(jié)果集與多重映射實(shí)例教程
Dapper類是一個開源的數(shù)據(jù)庫操作類,下面這篇文章主要給大家介紹了關(guān)于如何使用Dapper處理多個結(jié)果集與多重映射的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09C#判斷一個類是否實(shí)現(xiàn)了某個接口3種實(shí)現(xiàn)方法
這篇文章主要介紹了C#判斷一個類是否實(shí)現(xiàn)了某個接口3種實(shí)現(xiàn)方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06