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 = new List<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; int routNameColumn = 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;
}

