ExcelUtil2.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package com.gyee.impala.common.util;
  2. import org.apache.poi.hssf.usermodel.*;
  3. import org.apache.poi.ss.usermodel.Cell;
  4. import org.apache.poi.ss.usermodel.Row;
  5. import org.apache.poi.ss.usermodel.Sheet;
  6. import org.apache.poi.ss.usermodel.Workbook;
  7. import org.apache.poi.ss.util.CellRangeAddress;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. import java.text.SimpleDateFormat;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.List;
  16. public class ExcelUtil2 {
  17. public static void createExcel(List<String> header, List<String[]> data, OutputStream out) throws IOException {
  18. // 创建一个Excel文件
  19. HSSFWorkbook workbook = new HSSFWorkbook();
  20. // 创建一个工作表
  21. HSSFSheet sheet = workbook.createSheet("sheet1");
  22. // 添加表头行
  23. HSSFRow hssfRow = sheet.createRow(0);
  24. // 设置单元格格式居中
  25. HSSFCellStyle cellStyle = workbook.createCellStyle();
  26. // 添加表头内容
  27. for (int i = 0; i < header.size(); i++) {
  28. HSSFCell headCell = hssfRow.createCell(i);
  29. headCell.setCellValue(header.get(i));
  30. headCell.setCellStyle(cellStyle);
  31. }
  32. // 添加数据内容
  33. for (int i = 0; i < data.size(); i++) {
  34. String[] strings = data.get(i);
  35. hssfRow = sheet.createRow(i + 1);
  36. for (int j = 0; j < strings.length; j++) {
  37. HSSFCell cell = hssfRow.createCell(j);
  38. cell.setCellValue(strings[j]);
  39. cell.setCellStyle(cellStyle);
  40. }
  41. }
  42. //单元格自适应
  43. sheet.autoSizeColumn(2,true);
  44. // 保存Excel文件
  45. workbook.write(out);
  46. }
  47. public static void createExcel(String title,List<String> header, List<String[]> data, OutputStream out) throws IOException {
  48. // 创建一个Excel文件
  49. HSSFWorkbook workbook = new HSSFWorkbook();
  50. // 创建一个工作表
  51. HSSFSheet sheet = workbook.createSheet("sheet1");
  52. // 添加表头行
  53. HSSFRow hssfRow = sheet.createRow(0);
  54. HSSFCell cellTitle = hssfRow.createCell(0);
  55. // 设置标题外的单元格格式居中
  56. HSSFCellStyle cellStyle = workbook.createCellStyle();
  57. //设置标题的样式
  58. HSSFCellStyle titleCellStyle = workbook.createCellStyle();
  59. //设置标题字体的样式
  60. HSSFFont font = workbook.createFont();
  61. font.setFontHeightInPoints((short) 12);//设置字体大小
  62. titleCellStyle.setFont(font);
  63. //标题设置(四个参数分别表示起始行,终止行,起始列,终止列)
  64. cellTitle.setCellValue(title);
  65. int lastCol = header.size() > 1 ? header.size() : 2;
  66. CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) lastCol - 1);
  67. sheet.addMergedRegion(region1);
  68. hssfRow = sheet.createRow(1);
  69. hssfRow = sheet.createRow(2);
  70. cellTitle.setCellStyle(titleCellStyle);
  71. // 添加表头内容
  72. for (int i = 0; i < header.size(); i++) {
  73. HSSFCell headCell = hssfRow.createCell(i);
  74. headCell.setCellValue(header.get(i));
  75. headCell.setCellStyle(cellStyle);
  76. }
  77. // 添加数据内容
  78. for (int i = 0; i < data.size(); i++) {
  79. String[] strings = data.get(i);
  80. hssfRow = sheet.createRow(i + 3);
  81. for (int j = 0; j < strings.length; j++) {
  82. HSSFCell cell = hssfRow.createCell(j);
  83. cell.setCellValue(strings[j]);
  84. cell.setCellStyle(cellStyle);
  85. }
  86. }
  87. // 保存Excel文件
  88. workbook.write(out);
  89. }
  90. /**
  91. * 读取Excel的内容
  92. *
  93. * @param fileType 文件类型,xls或xlsx
  94. * @param startRows 开始读取行数,比喻行头不需要读入 忽略的行数为1
  95. * @param ignoreRowBlank 是否忽略空行
  96. * @param is 文件输入流
  97. * @return 读出的Excel中数据的内容
  98. * @throws IOException duxxxxx
  99. */
  100. public static List<String[]> readData(String fileType, int startRows, boolean ignoreRowBlank, InputStream is) throws IOException {
  101. List<String[]> result = new ArrayList<>();
  102. Workbook wb = readExcel(fileType, is);
  103. for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
  104. Sheet sheet = wb.getSheetAt(sheetIndex);
  105. for (int rowIndex = startRows, z = sheet.getLastRowNum(); rowIndex <= z; rowIndex++) {
  106. Row row = sheet.getRow(rowIndex);
  107. if (row == null) {
  108. continue;
  109. }
  110. int rowSize = sheet.getRow(0).getLastCellNum();
  111. String[] values = new String[rowSize];
  112. boolean hasValue = false;
  113. for (int columnIndex = 0; columnIndex < rowSize; columnIndex++) {
  114. String value = "";
  115. Cell cell = row.getCell(columnIndex);
  116. if (cell != null) {
  117. // 注意:一定要设成这个,否则可能会出现乱码,后面版本默认设置
  118. switch (cell.getCellType()) {
  119. case HSSFCell.CELL_TYPE_STRING:
  120. value = cell.getStringCellValue();
  121. break;
  122. case HSSFCell.CELL_TYPE_NUMERIC:
  123. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  124. Date date = cell.getDateCellValue();
  125. if (date != null) {
  126. value = new SimpleDateFormat("yyyy-MM-dd")
  127. .format(date);
  128. } else {
  129. value = "";
  130. }
  131. } else {
  132. //value = new DecimalFormat("0").format(cell.getNumericCellValue());
  133. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  134. value = String.valueOf(cell.getDateCellValue());
  135. } else {
  136. cell.setCellType(Cell.CELL_TYPE_STRING);
  137. String temp = cell.getStringCellValue();
  138. // 判断是否包含小数点,如果不含小数点,则以字符串读取,如果含小数点,则转换为Double类型的字符串
  139. if (temp.indexOf(".") > -1) {
  140. value = String.valueOf(new Double(temp)).trim();
  141. } else {
  142. value = temp.trim();
  143. }
  144. }
  145. }
  146. break;
  147. case HSSFCell.CELL_TYPE_FORMULA:
  148. // 导入时如果为公式生成的数据则无值
  149. if (!cell.getStringCellValue().equals("")) {
  150. value = cell.getStringCellValue();
  151. } else {
  152. value = cell.getNumericCellValue() + "";
  153. }
  154. break;
  155. case HSSFCell.CELL_TYPE_BLANK:
  156. break;
  157. case HSSFCell.CELL_TYPE_ERROR:
  158. value = "";
  159. break;
  160. case HSSFCell.CELL_TYPE_BOOLEAN:
  161. value = (cell.getBooleanCellValue() == true ? "Y"
  162. : "N");
  163. break;
  164. default:
  165. value = "";
  166. }
  167. }
  168. values[columnIndex] = value;
  169. if (!value.isEmpty()) {
  170. hasValue = true;
  171. }
  172. }
  173. if (!ignoreRowBlank || hasValue) {//不为忽略空行模式或不为空行
  174. result.add(values);
  175. }
  176. }
  177. }
  178. return result;
  179. }
  180. //读取excel
  181. private static Workbook readExcel(String fileType, InputStream is) throws IOException {
  182. if ("xls".equals(fileType)) {
  183. return new HSSFWorkbook(is);
  184. } else if ("xlsx".equals(fileType)) {
  185. return new XSSFWorkbook(is);
  186. } else {
  187. throw new IllegalArgumentException("不支持的文件类型,仅支持xls和xlsx");
  188. }
  189. }
  190. /**
  191. * 去掉字符串右边的空格
  192. *
  193. * @param str 要处理的字符串
  194. * @return 处理后的字符串
  195. */
  196. private static String rightTrim(String str) {
  197. if (str == null) {
  198. return "";
  199. }
  200. int length = str.length();
  201. for (int i = length - 1; i >= 0; i--) {
  202. if (str.charAt(i) != 0x20) {
  203. break;
  204. }
  205. length--;
  206. }
  207. return str.substring(0, length);
  208. }
  209. }