一、先来看看最常见的导入操作吧!

  1. private void Import()
  2. {
  3. //打开excel选择框 OpenFileDialog frm = new OpenFileDialog();
  4. frm.Filter = "Excel文件(*.xls,xlsx)|*.xls;*.xlsx";
  5. if (frm.ShowDialog() == DialogResult.OK)
  6. {
  7.  
  8. string excelName = frm.FileName;
  9. Workbook excel = new Workbook(excelName);
  10. List<string[]> importyString=GetImportExcelRoute(excel); }}
  1. //循环遍历获取excel的中每行每列的值 public List<string[]> GetImportExcelRoute(Workbook excel)
  2. {
  3. int icount = excel.Worksheets.Count;
  4.  
  5. List<string[]> routList = new List<string[]>();
  6. for (int i = 0; i < icount; i++)
  7. {
  8. Worksheet sheet = excel.Worksheets[i];
  9. Cells cells = sheet.Cells;
  10. int rowcount = cells.MaxRow;
  11. int columncount = cells.MaxColumn;
  12.  
  13. int routNameColumn = 0;
  14. int routAttachColumn = 0;
  15. int routDescColumn = 0;
  16. int routMesgColumn = 0;
  17.  
  18. //获取标题所在的列 if (rowcount > 0 && columncount > 0)
  19. {
  20. //找到对应的列信息
  21. int r0 = 2;
  22. for (int c = 0; c <= columncount; c++)
  23. {
  24. string strVal = cells[r0, c].StringValue.Trim();
  25. if (strVal == "备注")
  26. {
  27. routDescColumn = c;
  28. break;
  29. }
  30. }
  31.  
  32. r0 = 3;
  33. for (int c = 0; c <= columncount; c++)
  34. {
  35. //获取文本框内容
  36. string strVal = cells[r0, c].StringValue.Trim();
  37. if (strVal == "审批明细事项")
  38. {
  39. routNameColumn = c;
  40. }
  41. if (strVal == "细项")
  42. {
  43. routMesgColumn = c;
  44. }
  45. if (strVal == "前置条件及工作要求")
  46. {
  47. routAttachColumn = c;
  48. }
  49. }
  50. //找到对应标题列下面的值
  51. if (routNameColumn > 0 && routAttachColumn > 0 && routDescColumn > 0)
  52. {//在从对应的列中找到对应的值
  53. for (int r = 4; r <= rowcount; r++)
  54. {
  55. string[] str = new string[6];
  56. string strRoutName = "";
  57. string strRoutMesg = "";
  58. string strRoutAttach = "";
  59. string strRoutDesc = "";
  60. string strRoutRole = "";
  61. string strRoutPro = "";
  62. for (int c = 0; c <= columncount; c++)
  63. {
  64. int mergcolumncount = 0;
  65. int mergrowcount = 0;
  66.  
  67. bool ismerged = cells[r, c].IsMerged;//是否合并单元格
  68. if (ismerged)
  69. {
  70. Range range = cells[r, c].GetMergedRange();
  71.  
  72. if (range != null)
  73. {
  74. mergcolumncount = range.ColumnCount;
  75. mergrowcount = range.RowCount;
  76. }
  77. }
  78. //获取文本框内容
  79. string strVal = "";
  80. strVal = cells[r, c].StringValue.Trim();
  81. if (c == routNameColumn)
  82. {
  83. strRoutName = strVal;
  84. if (mergrowcount > 1 && string.IsNullOrEmpty(strRoutName))
  85. {
  86. strRoutName = GetRoutName(routList, 0);
  87. }
  88. }
  89. if (c == routMesgColumn)
  90. {
  91. strRoutMesg = strVal;
  92. if (mergrowcount > 1 && string.IsNullOrEmpty(strRoutMesg))
  93. {
  94. strRoutMesg = GetRoutName(routList, 1);
  95. }
  96. }
  97. if (c == routAttachColumn)
  98. {
  99. strRoutAttach = strVal;
  100. }
  101. if (c == routDescColumn)
  102. {
  103. strRoutDesc = strVal;
  104. }
  105. } } }

可以看到导入是比较简单的,就是循环读取每行每列的值,可以看到文中有不少Cells这个属性,这个需要用到第三方的插件:using Aspose.Cells;需要在网上下载一个 Aspose.Cells的dll.

二、导出,就是将数据组合好后导成excel格式:

  1. private void Export()
  2. {
  3. SaveFileDialog frm = new SaveFileDialog();
  4. frm.Filter = "Excel文件(*.xls,xlsx)|*.xls;*.xlsx";
  5. frm.FileName = flowName + ".xlsx";
  6. if (frm.ShowDialog() == DialogResult.OK)
  7. {
  8. string strpath = frm.FileName;
  9. 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="";//获取要导出的数据
  10.  
  11. try { RoutExportToExcel(workbook,cells,str);
  12.  
  13. MessageBox.Show("导出成功!");
  14. } catch {
  15. MessageBox.Show("导出失败!");
  16. }
  17. }
  18. }
  19. }
  1. public void RoutExportToExcel(Workbook workbook, Cells cells,string str)
  2. {
  3. 分别得到行和列
  4. int routCount =0;//;
  5. int rowcount = 4 + routCount;
  6. int columnCount = 25;
  7. for (int i = 0; i < rowcount; i++)
  8. {
  9. Style style = SettingCellStyle(workbook, cells);
  10. if (i == 0)
  11. {
  12. style.Font.Color = Color.Red;
  13. style.Font.Size = 16;
  14. cells.Merge(0, 0, 1, columnCount);//合并单元格
  15. cells[i, 0].PutValue("综合管线决策授权体系事项");//填写内容
  16. cells[0, 0].SetStyle(style);//给单元格关联样式
  17. cells.SetRowHeight(0, 38);//设置行高
  18. cells.SetColumnWidth(1, 20);//设置列宽
  19. }
  20. if (i > 0)
  21. {
  22. string routeName = "";
  23. string routeNote = "";
  24. string routeCondition = "";
  25. string guid = "";
  26. if (i > 3)
  27. {
  28. cells.SetRowHeight(i, 42);//设置行高
  29. JsonObject routJsonObj = routeJsonArray[i - 4] as JsonObject;
  30. routeName = routJsonObj["routName"] == null ? "" : routJsonObj["routName"].ToString();
  31. routeNote = routJsonObj["note"] == null ? "" : routJsonObj["note"].ToString();
  32. routeCondition = routJsonObj["condition"] == null ? "" : routJsonObj["condition"].ToString();
  33. guid = routJsonObj["guid"] == null ? "" : routJsonObj["guid"].ToString();
  34. }
  35. for (int j = 0; j < columnCount; j++)
  36. {
  37. cells[i, j].SetStyle(style);//给单元格关联样式
  38. //填充行
  39. if (i > 3)
  40. {
  41. if (j == 0)//序号
  42. {
  43. cells[i, j].PutValue(i - 3);//填写内容
  44. }
  45. else if (j == 4 || j == 5 || j == 24)//审批明细事项 细项 备注
  46. {
  47. FillExcelRoutProperty(i,j,style,cells,routeName,routeNote,routeCondition);
  48. }
  49. else if (j == 2 || j == 3 || j == 6 || j == 7 || j == 8 || j == 10 || j == 11 || j == 12 || j == 13)//类别、分类、层级或板块、地区、管线、前置条件责任部门及责任人、审核校对验收责任部门及责任人、具体审核校对验收要求、发起人
  50. {
  51. FillExcelRoutExtProperty(i, j, guid, style, cells,routExtPropertyJsonArray);
  52. }
  53. else if (j >= 14 && j <= 23)//路由角色变量(从审批人1到终审人)
  54. {
  55. FillExcelRoutRoleVal(i,j,guid,style,cells,routRoleValjsonArray);
  56. }
  57. else if (j == 9)//前置条件及工作要求
  58. {
  59. FillExcelRoutPreConditon(i,j,guid,style,cells,routPreConditonJsonArray);
  60. }
  61. }
  62. else
  63. {
  64. SettingCellStyleAndLine(cells, i, j);//设置excel的标题行和列
  65. }
  66. }
  67. }
  68. }
  69. }
  1. /// <summary>
  2. /// 设置单元格样式及线条
  3. /// </summary>
  4. /// <param name="cells"></param>
  5. /// <param name="i"></param>
  6. /// <param name="j"></param>
  7. public void SettingCellStyleAndLine(Cells cells, int i, int j)
  8. {
  9. if (i == 1)
  10. {
  11. if (j == 0)
  12. {
  13. cells.Merge(1, j, 3, 1);//合并单元格
  14. cells[i, j].PutValue("序号");//填写内容
  15. cells.SetColumnWidth(j, 5);//设置列宽
  16. }
  17. if (j == 1)
  18. {
  19. cells.Merge(1, j, 3, 1);//合并单元格
  20. cells.SetColumnWidth(j, 5);//设置列宽
  21. }
  22. }
  23. }
  1. /// <summary>
  2. /// 设置单元格的样式
  3. /// </summary>
  4. /// <param name="workbook"></param>
  5. /// <param name="cells"></param>
  6. /// <returns></returns>
  7. public Style SettingCellStyle(Workbook workbook, Cells cells)
  8. {
  9. Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
  10. style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
  11. style.Font.Name = "宋体";//文字字体
  12. style.Font.Size = 10;//文字大小
  13. style.IsLocked = false;//单元格解锁
  14. style.Font.IsBold = false;//粗体
  15. style.ForegroundColor = Color.FromArgb(255, 255, 255);//设置背景色
  16. style.Pattern = BackgroundType.Solid; //设置背景样式
  17. style.IsTextWrapped = true;//单元格内容自动换行
  18. style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //应用边界线 左边界线
  19. style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
  20. style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //应用边界线 上边界线
  21. style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //应用边界线 下边界线
  22. return style;
  23. }

在C#中关于excel的导入和导出操作的更多相关文章

  1. SpringBoot中关于Excel的导入和导出

    前言   由于在最近的项目中使用Excel导入和导出较为频繁,以此篇博客作为记录,方便日后查阅.本文前台页面将使用layui,来演示对Excel文件导入和导出的效果.本文代码已上传至我的gitHub, ...

  2. 表格类型数据,Excel csv导入,导出操作

    import pandas # 创建表格格式# ad = pandas.DataFrame({"a": range(1, 10), "b": range(10, ...

  3. 前端必读3.0:如何在 Angular 中使用SpreadJS实现导入和导出 Excel 文件

    在之前的文章中,我们为大家分别详细介绍了在JavaScript.React中使用SpreadJS导入和导出Excel文件的方法,作为带给广大前端开发者的"三部曲",本文我们将为大家 ...

  4. excel的导入与导出---通用版

    excel的导入与导出---通用版 web项目关于导入导出的业务场景很常见,最近我就又遇到了这个业务场景.这次将最近半个月做的导入导出总结一下 使用的pom如下,主要还是阿里巴巴的easyexcel依 ...

  5. .net数据库实现Excel的导入与导出

    .net数据库实现Excel的导入与导出 参考路径:https://www.cnblogs.com/splendidme/archive/2012/01/05/2313314.html 1.defau ...

  6. C#中Excel的导入和导出的几种基本方式

    在上一篇(http://www.cnblogs.com/fengchengjushi/p/3369386.html)介绍过,Excel也是数据持久化的一种实现方式.在C#中.我们常常会与Excel文件 ...

  7. Excel报表开发(主要讲Excel的导入和导出)

    一.Excel数据导入 连接字符串Excel2003版: OleDbConnection conn = new OleDbConnection("provider=Microsoft.Jet ...

  8. java实现Excel的导入、导出

    一.Excel的导入 导入可采用两种方式,一种是JXL,另一种是POI,但前者不能读取高版本的Excel(07以上),后者更具兼容性.由于对两种方式都进行了尝试,就都贴出来分享(若有错误,请给予指正) ...

  9. SpringBoot整合easyexcel实现Excel的导入与导出

    导出 在一般不管大的或者小的系统中,各家的产品都一样,闲的无聊的时候都喜欢让我们这些程序员导出一些数据出来供他观赏,非说这是必须需求,非做不可,那么我们就只能苦逼的哼哧哼哧的写bug喽. 之前使用PO ...

随机推荐

  1. [转]让你提升命令行效率的 Bash 快捷键

    生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向) ...

  2. tcl/tk

    http://blog.csdn.net/dulixin/article/category/365058 http://blog.csdn.net/dulixin/article/category/3 ...

  3. yii下使用oracle中文都变成问号乱码的解决方法

    在配置文件中,一般会用以下配置: 'db_oracle'=>array( 'class' => 'CDbConnection', 'connectionString'=>'oci:d ...

  4. Hive体系结构介绍

    http://www.aboutyun.com/thread-6217-1-1.html   1.Hive架构与基本组成     下面是Hive的架构图. 图1.1 Hive体系结构     Hive ...

  5. redhat ping不通外网的解决办法

    ping自己和网关都能ping通就是无法ping通外网例如百度: [root@ocdp1 ~]# ping www.baidu.com ping: unknown host www.baidu.com ...

  6. Appium 提高脚本复用、可配置性

  7. XCode实用好用插件集锦

    工欲善其事,必先利其器,iOS开发运用插件可以大大提高开发的效率. 原文地址: http://www.oschina.net/project/tag/432/xcode-plugins

  8. 12天学好C语言——记录我的C语言学习之路(Day 4)

    12天学好C语言--记录我的C语言学习之路 Day 4: 首先来看一段程序: //输出下面4*5的矩阵 /* 1  2  3   4   5 2  4  6   8   10 3  6  9   12 ...

  9. Windows下查询进程、端口

    PID --> 端口号netstat -ano | findstr 8244 端口号 --> PIDnetstat -aon|findstr "11211" PID - ...

  10. a标签根据js返回值判断页面是否跳转

    a标签再跳转之前先判断是否符合条件,符合可以跳转,不符合不可以跳转. 自己遇到的问题是:在js方法中根据条件就return结果,但是不行. 原因是:在js方法中return后不会结束整个js方法(ac ...