網站首頁 編程語言 正文
本文實例為大家分享了C#使用NPOI實現Excel導入導出的具體代碼,供大家參考,具體內容如下
Excel導入
使用OpenFileDiolog控件和button結合,選擇文件導入,將路徑顯示在文本框
設置按鈕點擊事件,將文件路徑賦給textBox.Text
private void Department_SUM_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? OpenFileDialog open = new OpenFileDialog(); ? ? ? ? ? ? open.ShowDialog(); ? ? ? ? ? ? textBox1.Text = open.FileName; ? ? ? ? }
實現excel導入,通過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列數據,如果cell類型是文本,則通過StringCellValue取值 //如果cell類型是數值,則通過NumericCellValue來取值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? row = sheet.GetRow(i + 1); ? ? ? ? ? ? ? ? ? ? ? ? ? ?row.Cells[5].NumericCellValue; ?? ??? ??? ??? ??? ??? ?row.Cells[6].StringCellValue; //可以將Cell的數據存放在list中,這里假設將兩列cell的數據存入list1,list2 ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? fs.Close(); ? ? ? ? ? ? ? ? ? ? } ?? ??? ?//實際存放DataTable的位置 ?? ??? ?//調用自定義方法,實現導出 ?? ??? ?Add_DataTable_To_Excel(txtpath, table, sheet_name); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }
要實現excel導出,先將程序中的excel存為DataTable格式
本段代碼存在于上面代碼“//實際存放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"); //將存入的數據格式保存為保留四位小數 ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? table.Rows.Add(dr); ? ? ? ? ? ? }
通過方法導出excel,傳參為文件路徑,DataTable,表名
通過獲取要導入數據的目標excel的內容,導入數據,要將excel導出的方式
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); //設置馬上要使用的Cell數據格式 ? ? ? ? ? ? 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中的數據存入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; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } } } //導出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; }
基礎的Excel文件的導入導出功能到這里全部完成
原文鏈接:https://blog.csdn.net/qq_48591625/article/details/108190904
相關推薦
- 2022-04-17 使用css漸變色。實現動態進度條效果
- 2022-08-12 Go?內聯優化讓程序員愛不釋手_Golang
- 2022-12-06 R語言如何畫豎線、橫線、添加標簽以及畫固定長度的線段_R語言
- 2022-06-08 Python?全局空間和局部空間_python
- 2022-05-24 C語言的strcpy函數你了解嗎_C 語言
- 2022-05-06 如何利用Python處理excel表格中的數據_python
- 2023-07-09 【elementplus】body設置zoom后,el-table開啟show-overflow-t
- 2022-06-15 axios?gin的GET和POST請求實現示例_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支