说明

Excel操作(ExcelUtil) ExcelUtil 操作Excel工具类

常用方法

AddCellFontColor 设置单元格样式的字体颜色

AddCellFontWeight 设置单元格样式的字体加粗

AddCellForegroundColor 设置单元格样式的前景色

AddCellFontSize 设置单元格样式的字体大小

AddCellHorizontalAlignmentRight 设置单元格样式的水平对齐属性为靠右

AddCellHorizontalAlignmentCenter 设置单元格样式的水平对齐属性为居中

AddCellHorizontalAlignmentLeft 设置单元格样式的水平对齐属性为靠左

AddCellVerticalAlignmentBottom 设置单元格样式的垂直对齐属性为靠下

AddCellVerticalAlignmentCenter 设置单元格样式的垂直对齐属性为居中

AddCellVerticalAlignmentTop 设置单元格样式的垂直对齐属性为靠上

AddCellBorderLeftThin 设置单元格样式的左边框为thin样式

AddCellBorderBottomThin 设置单元格样式的下边框为thin样式

AddCellBorderTopThin 设置单元格样式的上边框为thin样式

AddCellBorderRightThin 设置单元格样式的右边框为thin样式

AddCellsSingleStyle 往配置对象中添加单元格样式

AddRowHeightStyle 导出配置中添加行高

AddColWidthStyle 导出配置中添加列宽

AddColDataTypeBool 导出配置中 设置列的数据类型 为布尔型(bool)

AddColDataTypeDate 导出配置中 设置列的数据类型 为日期型(date)

AddColDataTypeString 导出配置中 设置列的数据类型 为字符串(string)

AddColDataTypeNumeric 导出配置中 设置列的数据类型 为数值型(numeric)

AddBodyCellStyle 导出配置中添加表格内容样式

AddHeadCellStyle 导出配置中添加表格头样式

ImportFromExcelRequest 从http请求文件流中获取文件,并导入为哈希对象

ImportFromExcelFile 将excel中的数据导入为哈希对象

ExportSingleSheetHeadBodyToExcelResponse 导出单个sheet到http响应流 单个sheet数据分为表格头和表格内容的形式

ExportSingleSheetHeadBodyToExcelFile 导出单个sheet为Excel文件 单个sheet数据分为表格头和表格内容的形式

ExportSingleSheetToExcelResponse 导出单个sheet到http响应流

ExportSingleSheetToExcelFile 导出单个sheet为Excel文件

ExportMutiSheetToExcelResponse 导出多个sheet为http响应流

ExportMutiSheetToExcelFile 导出多个sheet为Excel文件

示例

  1. //Excel操作测试
  2. /**
  3. * 测试添加头部单元格样式
  4. *
  5. * @param cell 头部单元格
  6. */
  7. private void TestAddHeadCell(ZmHash cell)
  8. {
  9. //设置字体
  10. ExcelUtil.AddCellFontSize(cell, 24);
  11. //设置加粗
  12. ExcelUtil.AddCellFontWeight(cell, true);
  13. //字体颜色
  14. ExcelUtil.AddCellFontColor(cell, "FF0000");
  15. //单元格前景色
  16. ExcelUtil.AddCellForegroundColor(cell, "00FF00");
  17. //设置垂直靠上
  18. ExcelUtil.AddCellVerticalAlignmentTop(cell);
  19. //水平靠右
  20. ExcelUtil.AddCellHorizontalAlignmentRight(cell);
  21. //设置上下左右的边框 为thin样式
  22. ExcelUtil.AddCellBorderRightThin(cell);
  23. ExcelUtil.AddCellBorderLeftThin(cell);
  24. ExcelUtil.AddCellBorderTopThin(cell);
  25. ExcelUtil.AddCellBorderBottomThin(cell);
  26. }
  27. /**
  28. * excel导出
  29. *
  30. */
  31. @RequestMapping("exportExcel")
  32. @ResponseBody
  33. public void exportExcel()
  34. {
  35. ZmActiveRequest zmRequest = base.getActiveRequest();
  36. //excel头
  37. ZmHash strHash = new ZmHash();
  38. strHash.Add("name", "姓名");
  39. strHash.Add("age", "年龄");
  40. //excel内容
  41. ZmHashList dataList = new ZmHashList();
  42. ZmHash hash = new ZmHash();
  43. hash.Add("name", "zhangsan");
  44. hash.Add("age", 18);
  45. dataList.Add(hash);
  46. hash = new ZmHash();
  47. hash.Add("name", "lisi");
  48. hash.Add("age", 32);
  49. dataList.Add(hash);
  50. //excel文件名称
  51. string fileName = "testsheet.xlsx";
  52. //导出参数
  53. ZmHash configs = new ZmHash();
  54. //头部单元格样式
  55. ZmHash headCell = new ZmHash();
  56. this.TestAddHeadCell(headCell);
  57. //设置单元格样式
  58. ExcelUtil.AddBodyCellStyle(configs, headCell);
  59. //行高
  60. ExcelUtil.AddRowHeightStyle(configs, 1, 100);
  61. ExcelUtil.AddRowHeightStyle(configs, 2, 100);
  62. //设置列宽
  63. ExcelUtil.AddColWidthStyle(configs, "name", 100);
  64. //设置列宽
  65. ExcelUtil.AddColWidthStyle(configs, "age", 100);
  66. //单个sheet的内容导出到http响应流内
  67. ExcelUtil.ExportSingleSheetHeadBodyToExcelResponse(strHash, dataList, "testsheet",
  68. configs, fileName, zmRequest);
  69. }
  70. /**
  71. * excel导入
  72. *
  73. */
  74. @RequestMapping("importExcel")
  75. @ResponseBody
  76. public ZmHash importExcel()
  77. {
  78. ZmActiveRequest zmRequest = base.getActiveRequest();
  79. //从http请求流中导入Excel
  80. ZmReturnMsg returnMsg = ExcelUtil.ImportFromExcelRequest(zmRequest);
  81. //会包含一个sheetList字段,内容为导入的excel列表
  82. returnMsg.Add("Result", true);
  83. returnMsg.Add("Message", returnMsg.MsgContent);
  84. returnMsg.Add("SNO", returnMsg.OtherData);
  85. return returnMsg;
  86. }

示例视频包含的内容

Excel的基本操作

Excel 文件的导入

Excel 文件的导出