在C#中關於excel的匯入和匯出操作
一、先來看看最常見的匯入操作吧!
private void Import() { //開啟excel選擇框
OpenFileDialog frm = new OpenFileDialog(); frm.Filter = "Excel檔案(*.xls,xlsx)|*.xls;*.xlsx"; if (frm.ShowDialog() == DialogResult.OK) { string excelName = frm.FileName; Workbook excel= new Workbook(excelName); List<string[]> importyString=GetImportExcelRoute(excel);
}
}
//迴圈遍歷獲取excel的中每行每列的值
public List<string[]> GetImportExcelRoute(Workbook excel) { int icount = excel.Worksheets.Count; List<string[]> routList = newList<string[]>(); for (int i = 0; i < icount; i++) { Worksheet sheet = excel.Worksheets[i]; Cells cells = sheet.Cells; int rowcount = cells.MaxRow; int columncount = cells.MaxColumn; introutNameColumn = 0; int routAttachColumn = 0; int routDescColumn = 0; int routMesgColumn = 0; //獲取標題所在的列
if (rowcount > 0 && columncount > 0) { //找到對應的列資訊 int r0 = 2; for (int c = 0; c <= columncount; c++) { string strVal = cells[r0, c].StringValue.Trim(); if (strVal == "備註") { routDescColumn = c; break; } } r0 = 3; for (int c = 0; c <= columncount; c++) { //獲取文字框內容 string strVal = cells[r0, c].StringValue.Trim(); if (strVal == "審批明細事項") { routNameColumn = c; } if (strVal == "細項") { routMesgColumn = c; } if (strVal == "前置條件及工作要求") { routAttachColumn = c; } } //找到對應標題列下面的值 if (routNameColumn > 0 && routAttachColumn > 0 && routDescColumn > 0) {//在從對應的列中找到對應的值 for (int r = 4; r <= rowcount; r++) { string[] str = new string[6]; string strRoutName = ""; string strRoutMesg = ""; string strRoutAttach = ""; string strRoutDesc = ""; string strRoutRole = ""; string strRoutPro = ""; for (int c = 0; c <= columncount; c++) { int mergcolumncount = 0; int mergrowcount = 0; bool ismerged = cells[r, c].IsMerged;//是否合併單元格 if (ismerged) { Range range = cells[r, c].GetMergedRange(); if (range != null) { mergcolumncount = range.ColumnCount; mergrowcount = range.RowCount; } } //獲取文字框內容 string strVal = ""; strVal = cells[r, c].StringValue.Trim(); if (c == routNameColumn) { strRoutName = strVal; if (mergrowcount > 1 && string.IsNullOrEmpty(strRoutName)) { strRoutName = GetRoutName(routList, 0); } } if (c == routMesgColumn) { strRoutMesg = strVal; if (mergrowcount > 1 && string.IsNullOrEmpty(strRoutMesg)) { strRoutMesg = GetRoutName(routList, 1); } } if (c == routAttachColumn) { strRoutAttach = strVal; } if (c == routDescColumn) { strRoutDesc = strVal; }
}
}
}
private string GetRoutName(List<string[]> routList, int index)
{
if (routList == null)
return "";
if (routList.Count == 0)
return "";
return routList[routList.Count - 1][index];
}
可以看到匯入是比較簡單的,就是迴圈讀取每行每列的值,可以看到文中有不少Cells這個屬性,這個需要用到第三方的外掛:using Aspose.Cells;需要在網上下載一個 Aspose.Cells的dll.
二、匯出,就是將資料組合好後導成excel格式:
private void Export() { SaveFileDialog frm = new SaveFileDialog(); frm.Filter = "Excel檔案(*.xls,xlsx)|*.xls;*.xlsx"; frm.FileName = flowName + ".xlsx"; if (frm.ShowDialog() == DialogResult.OK) { string strpath = frm.FileName; Workbook workbook = null;
string strpath = _exportFlowRoutExcelPath;
if (File.Exists(strpath))
{
workbook = new Workbook(strpath);
}
else
{
workbook = new Workbook();
}
Worksheet sheet = workbook.Worksheets[0]; //工作表
Cells cells = sheet.Cells;//單元格
string str="";//獲取要匯出的資料
try
{
RoutExportToExcel(workbook,cells,str);
MessageBox.Show("匯出成功!"); }
catch
{ MessageBox.Show("匯出失敗!"); } } } }
public void RoutExportToExcel(Workbook workbook, Cells cells,string str) { 分別得到行和列 int routCount =0;//; int rowcount = 4 + routCount; int columnCount = 25; for (int i = 0; i < rowcount; i++) { Style style = SettingCellStyle(workbook, cells); if (i == 0) { style.Font.Color = Color.Red; style.Font.Size = 16; cells.Merge(0, 0, 1, columnCount);//合併單元格 cells[i, 0].PutValue("綜合管線決策授權體系事項");//填寫內容 cells[0, 0].SetStyle(style);//給單元格關聯樣式 cells.SetRowHeight(0, 38);//設定行高 cells.SetColumnWidth(1, 20);//設定列寬 } if (i > 0) { string routeName = ""; string routeNote = ""; string routeCondition = ""; string guid = ""; if (i > 3) { cells.SetRowHeight(i, 42);//設定行高 JsonObject routJsonObj = routeJsonArray[i - 4] as JsonObject; routeName = routJsonObj["routName"] == null ? "" : routJsonObj["routName"].ToString(); routeNote = routJsonObj["note"] == null ? "" : routJsonObj["note"].ToString(); routeCondition = routJsonObj["condition"] == null ? "" : routJsonObj["condition"].ToString(); guid = routJsonObj["guid"] == null ? "" : routJsonObj["guid"].ToString(); } for (int j = 0; j < columnCount; j++) { cells[i, j].SetStyle(style);//給單元格關聯樣式 //填充行 if (i > 3) { if (j == 0)//序號 { cells[i, j].PutValue(i - 3);//填寫內容 } else if (j == 4 || j == 5 || j == 24)//審批明細事項 細項 備註 { FillExcelRoutProperty(i,j,style,cells,routeName,routeNote,routeCondition); } else if (j == 2 || j == 3 || j == 6 || j == 7 || j == 8 || j == 10 || j == 11 || j == 12 || j == 13)//類別、分類、層級或板塊、地區、管線、前置條件責任部門及責任人、稽核校對驗收責任部門及責任人、具體稽核校對驗收要求、發起人 { FillExcelRoutExtProperty(i, j, guid, style, cells,routExtPropertyJsonArray); } else if (j >= 14 && j <= 23)//路由角色變數(從審批人1到終審人) { FillExcelRoutRoleVal(i,j,guid,style,cells,routRoleValjsonArray); } else if (j == 9)//前置條件及工作要求 { FillExcelRoutPreConditon(i,j,guid,style,cells,routPreConditonJsonArray); } } else { SettingCellStyleAndLine(cells, i, j);//設定excel的標題行和列 } } } } }
/// <summary> /// 設定單元格樣式及線條 /// </summary> /// <param name="cells"></param> /// <param name="i"></param> /// <param name="j"></param> public void SettingCellStyleAndLine(Cells cells, int i, int j) { if (i == 1) { if (j == 0) { cells.Merge(1, j, 3, 1);//合併單元格 cells[i, j].PutValue("序號");//填寫內容 cells.SetColumnWidth(j, 5);//設定列寬 } if (j == 1) { cells.Merge(1, j, 3, 1);//合併單元格 cells.SetColumnWidth(j, 5);//設定列寬 } } }
/// <summary> /// 設定單元格的樣式 /// </summary> /// <param name="workbook"></param> /// <param name="cells"></param> /// <returns></returns> public Style SettingCellStyle(Workbook workbook, Cells cells) { Style style = workbook.Styles[workbook.Styles.Add()];//新增樣式 style.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style.Font.Name = "宋體";//文字字型 style.Font.Size = 10;//文字大小 style.IsLocked = false;//單元格解鎖 style.Font.IsBold = false;//粗體 style.ForegroundColor = Color.FromArgb(255, 255, 255);//設定背景色 style.Pattern = BackgroundType.Solid; //設定背景樣式 style.IsTextWrapped = true;//單元格內容自動換行 style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //應用邊界線 左邊界線 style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //應用邊界線 右邊界線 style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //應用邊界線 上邊界線 style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //應用邊界線 下邊界線 return style; }不積跬步,無以至千里!
相關推薦
在C#中關於excel的匯入和匯出操作
一、先來看看最常見的匯入操作吧!private void Import() { //開啟excel選擇框 OpenFileDialog frm = new OpenFileDi
Apache POI元件操作Excel(匯入和匯出)
Apache的POI元件是Java操作Microsoft Office辦公套件的強大API,其中對Word,Excel和PowperPoint都有支援,當然使用較多的還是Excel,因為Word和PowerPoint用程式動態操作的應用較少。那麼本文就結合POI來介紹一下
ASP.NET MVC5+EF6+EasyUI 後臺管理系統(88)-Excel匯入和匯出-主從表結構匯出
前言 前面一篇詳細講解了匯入匯出,本節演示混合結構的匯出功能!同時提供程式碼下載.. 先看效果圖:這個一個混合的Excel,列表與自定義資訊的混合! 我們的步驟大概分為以下幾步 1.模擬資料庫資料 2.建立工作簿 3.填充固定資料 4.合併單元格 5.處理動態資料
Java中Excel表格資料的匯入和匯出步驟和方法
Java Excel API既可以從本地檔案系統的一個檔案(.xls),也可以從輸入流中讀取Excel資料表。讀取Excel資料表的第一步是建立Workbook(術 語:工作薄),下面的程式碼片段舉例說明了應該如何操作:(完整程式碼見ExcelReading.java)
Excel的匯入和匯出功能
工作中經常會用到excel的匯入和匯出功能,這裡我提供匯入和匯出類。 匯入類(需要注意的地方我註釋裡面寫好了): package cn.teacheredu.utils;
SpringBoot通過WorkBook快速實現對Excel的匯入和匯出(包括資料校驗)
之前轉載過一篇對Excel基本操作相關的文章,這篇文章的瀏覽量迅速飆升,以至於在我部落格的熱門文章中排到了第三的位置,不過那篇轉載的文章實用性差並且講解不是很清晰,所以打算趁著今天休息,寫一篇關於SpringBoot通過WorkBook快速實現對Excel的匯入、匯出、資料校驗的文章,也是便於日後查
Vue中excel匯入匯出
npm安裝 npm install -S file-saver xlsx(這裡其實安裝了2個依賴) npm install -D script-loader 在src下建立一個資料夾,包含兩個檔案: Blob.js,Export2Excel.js下載地址:https://gitee
Hive的DML操作資料的匯入和匯出
Hive的DML資料操作 資料的匯入 向表中裝載資料(load):: 語法 hive>load data [local] inpath ‘/opt/module/datas/student.txt’ [overwrite] into table studen
如何在MySQL中匯入和匯出資料庫並重置root密碼
英文原作者:Etel Sverdlov 翻譯&轉載來源:https://www.digitalocean.com/community/tutorials/how-to-import-and-export-databases-and-reset-a-root-password-in
【MATLAB】匯入和匯出Excel資料
1 寫在前面 MATLAB 中有時候需要用到外部的資料進行模擬計算,或者有時需要把 MATLAB 中的資料匯出到外部表格,這就用到 Excel 與 MATLAB 之間的命令函式。 2 匯入 A=xlsread('xxx.xlsx') # xxx 為檔名稱 其中
jFinal中excel匯入匯出
jFinal中excel匯入匯出 Jfinal剛接觸不久,之前接觸的java框架基本上是jeesite,所以,當接到excel匯入及模板下載任務時,第一個想到的還是jeesite對Apache POI 3.9的簡單封裝,實現Excel的匯入匯出功能(只適用於簡單的匯入匯出,不合適複雜的表格或使
SpringBoot利用java反射機制,實現靈活讀取Excel表格中的資料和匯出資料至Excel表格
如果直接把固定Excel表格模板中的資料匯入資料庫,直接將Excel中的資料讀取出來,再存入資料庫中即可,將資料匯出至固定的Excel模板方法類似。但是,有時候,開發製作的模板與客戶需要的往往有些許差別(欄位排版、欄位數量等)。為了實現匯入匯出的靈活性,將利用java的反射機
Java實現Excel的匯入和匯出
一、下載jxl.jar包匯入到工程中 二、新建Book.java package com.cc.reflection; public class Book { private int id; private String name; private Strin
C# Excel匯入、匯出【原始碼下載】
本篇主要介紹C#的Excel匯入、匯出。 目錄 1. 介紹:描述第三方類庫NPOI以及Excel結構 2. Excel匯入:介紹C#如何呼叫NPOI進行Excel匯入,包含:流程圖、NOPI以及C#程式碼 3. Excel匯出:介紹C#如何呼叫NPOI進行Excel匯出,包含:流程圖、NOPI以、
Others8_如何在Chrome中匯入和匯出密碼
很多瀏覽器都可以儲存網站常用資訊表單和登入密碼,以幫助使用者快速填寫資訊和登入網站,筆者常用的 Chrome 瀏覽器也自不例外,但最近有訊息暴出了 Chrome 的密碼管理存在一些安全問題(無所謂啦,反正我不用)。如果你打算換到其它瀏覽器或使用像 Lastpass、1Pa
帝國建站系統中的模板匯入和匯出的問題
幾天的對帝國系統的應用,大概掌握了這個系統的執行機制,一般的簡單的小型網站,現在已經可以完成製作,但是對於大型的帶有論壇等一些互動功能的網站,自己還是做不來的。這幾天對帝國的網站的匯入和匯出做了細緻的學習,看了幾個人錄製的視訊,對帝國模板的匯入匯出算是學
使用PHPExcel實現Excel檔案的匯入和匯出
在之前有寫過一篇文章講述了使用,這種方式生成Excel檔案,生成速度很快,但是有缺點是: 1.單純的生成Excel檔案,生成的檔案沒有樣式,單元格屬性(填充色,寬度,高度,邊框顏色...)不能自定義; 2.生成的檔案雖然可以開啟,但是相容性很差,每次開啟,都會報一個警告:今
匯入和匯出Excel通用方法
package com.javen.service; import java.io.File; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List
王昕的 java 下Excel的匯入和匯出,資料校驗
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import j
Excel的匯入和匯出功能實現
工作中經常會用到excel的匯入和匯出功能,這裡我提供匯入和匯出類。 匯入類(需要注意的地方我註釋裡面寫好了): package cn.teacheredu.utils; import java.io.InputStream; import java.text.Simpl