PoiUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. package com.jtgh.yjpt.common;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.math.BigDecimal;
  5. import java.sql.Timestamp;
  6. import java.text.DateFormat;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import org.apache.commons.lang.StringUtils;
  11. import org.apache.poi.hssf.usermodel.HSSFCell;
  12. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  13. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  14. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  15. import org.apache.poi.hssf.usermodel.HSSFFont;
  16. import org.apache.poi.hssf.usermodel.HSSFFooter;
  17. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  18. import org.apache.poi.hssf.usermodel.HSSFRow;
  19. import org.apache.poi.hssf.usermodel.HSSFSheet;
  20. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  21. import org.apache.poi.ss.util.CellRangeAddress;
  22. import org.apache.poi.ss.util.NumberToTextConverter;
  23. public class PoiUtils {
  24. public static HSSFCell getCell(HSSFSheet sheet, int x, int y) {
  25. HSSFRow row = sheet.getRow(x - 1);
  26. if (row == null)
  27. return null;
  28. HSSFCell cell = row.getCell(y - 1);
  29. if (cell == null)
  30. return null;
  31. return cell;
  32. }
  33. /**
  34. * 取单元格内的数字,非数字的单元格返回0
  35. */
  36. public static double getNumericCellValue(HSSFCell cell) {
  37. if (cell == null)
  38. return 0;
  39. switch (cell.getCellType()) {
  40. case HSSFCell.CELL_TYPE_STRING: {
  41. try {
  42. return Double.parseDouble(StringUtils.trim(cell
  43. .getStringCellValue()));
  44. } catch (NumberFormatException pe) {
  45. return 0;
  46. }
  47. }
  48. case HSSFCell.CELL_TYPE_NUMERIC:
  49. return cell.getNumericCellValue();
  50. case HSSFCell.CELL_TYPE_FORMULA:
  51. return cell.getNumericCellValue();
  52. default:
  53. return 0;
  54. }
  55. }
  56. /**
  57. * 取单元格内的日期,非日期的单元格返回null
  58. */
  59. public static Date getDateCellValue(HSSFCell cell) {
  60. if (cell == null)
  61. return null;
  62. switch (cell.getCellType()) {
  63. case HSSFCell.CELL_TYPE_STRING: {
  64. try {
  65. DateFormat df = DateFormat.getDateInstance();
  66. return df.parse(cell.getStringCellValue());
  67. } catch (ParseException pe) {
  68. return null;
  69. }
  70. }
  71. case HSSFCell.CELL_TYPE_NUMERIC:
  72. if (HSSFDateUtil.isValidExcelDate(cell.getNumericCellValue()))
  73. return cell.getDateCellValue();
  74. case HSSFCell.CELL_TYPE_FORMULA:
  75. if (HSSFDateUtil.isValidExcelDate(cell.getNumericCellValue()))
  76. return cell.getDateCellValue();
  77. default:
  78. return null;
  79. }
  80. }
  81. /**
  82. * 取单元格内的日期,非日期的单元格返回null
  83. *
  84. * @throws ParseException
  85. * add by liuqi at 20110117
  86. */
  87. public static Timestamp getTimestampFromStringCellValue(HSSFCell cell)
  88. throws ParseException {
  89. if (cell.getCellType() != HSSFCell.CELL_TYPE_STRING)
  90. return null;
  91. if (cell.getStringCellValue().equals("")
  92. || cell.getStringCellValue() == null)
  93. return null;
  94. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  95. Date date = sdf.parse(cell.getStringCellValue());
  96. if (date == null)
  97. return null;
  98. else {
  99. Timestamp time = new Timestamp(date.getTime());
  100. return time;
  101. }
  102. }
  103. /**
  104. * 取单元格内的字符串,非字符串的单元格返回""
  105. */
  106. public static String getStringCellValue(HSSFCell cell) {
  107. if (cell == null)
  108. return "";
  109. switch (cell.getCellType()) {
  110. case HSSFCell.CELL_TYPE_BLANK:
  111. return "";
  112. case HSSFCell.CELL_TYPE_BOOLEAN:
  113. return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
  114. case HSSFCell.CELL_TYPE_STRING:
  115. return cell.getStringCellValue();
  116. case HSSFCell.CELL_TYPE_NUMERIC:
  117. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  118. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  119. return sdf.format(cell.getDateCellValue());
  120. }
  121. return NumberToTextConverter.toText(cell.getNumericCellValue());
  122. case HSSFCell.CELL_TYPE_FORMULA:
  123. return cell.getStringCellValue();
  124. default:
  125. return "";
  126. }
  127. }
  128. public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
  129. String strValue) {
  130. HSSFRow row = sheet.getRow(x - 1);
  131. if (row == null)
  132. row = sheet.createRow(x - 1);
  133. HSSFCell cell = row.getCell(y - 1);
  134. if (cell == null)
  135. cell = row.createCell(y - 1);
  136. // if (HSSFCell.CELL_TYPE_STRING != cell.getCellType())
  137. // cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  138. // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  139. HSSFRichTextString fts = new HSSFRichTextString(strValue);
  140. cell.setCellValue(fts);
  141. return cell;
  142. }
  143. public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
  144. boolean blValue) {
  145. HSSFRow row = sheet.getRow(x - 1);
  146. if (row == null)
  147. row = sheet.createRow(x - 1);
  148. HSSFCell cell = row.getCell(y - 1);
  149. if (cell == null)
  150. cell = row.createCell(y - 1);
  151. // cell.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
  152. cell.setCellValue(blValue);
  153. return cell;
  154. }
  155. public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
  156. int nValue) {
  157. HSSFRow row = sheet.getRow(x - 1);
  158. if (row == null)
  159. row = sheet.createRow(x - 1);
  160. HSSFCell cell = row.getCell(y - 1);
  161. if (cell == null)
  162. cell = row.createCell(y - 1);
  163. // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
  164. cell.setCellValue(nValue);
  165. return cell;
  166. }
  167. public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
  168. double dValue) {
  169. HSSFRow row = sheet.getRow(x - 1);
  170. if (row == null)
  171. row = sheet.createRow(x - 1);
  172. HSSFCell cell = row.getCell(y - 1);
  173. if (cell == null)
  174. cell = row.createCell(y - 1);
  175. // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
  176. cell.setCellValue(dValue);
  177. return cell;
  178. }
  179. public static HSSFCell setCellValue(HSSFSheet sheet, int x, int y,
  180. BigDecimal bValue) {
  181. HSSFRow row = sheet.getRow(x - 1);
  182. if (row == null)
  183. row = sheet.createRow(x - 1);
  184. HSSFCell cell = row.getCell(y - 1);
  185. if (cell == null)
  186. cell = row.createCell(y - 1);
  187. // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
  188. if (bValue != null)
  189. cell.setCellValue(bValue.doubleValue());
  190. else
  191. cell.setCellValue(0);
  192. return cell;
  193. }
  194. public static HSSFCell setCellFormula(HSSFSheet sheet, int x, int y,
  195. String strFormula) {
  196. HSSFRow row = sheet.getRow(x - 1);
  197. if (row == null)
  198. row = sheet.createRow(x - 1);
  199. HSSFCell cell = row.getCell(y - 1);
  200. if (cell == null)
  201. cell = row.createCell(y - 1);
  202. // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
  203. cell.setCellFormula(strFormula);
  204. return cell;
  205. }
  206. public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
  207. HSSFFont font) {
  208. if (cell == null)
  209. return false;
  210. HSSFCellStyle cellStyle = wb.createCellStyle();
  211. cellStyle.setFont(font);
  212. cell.setCellStyle(cellStyle);
  213. return true;
  214. }
  215. public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
  216. HSSFFont font, String strFormat) {
  217. if (cell == null)
  218. return false;
  219. HSSFCellStyle cellStyle = wb.createCellStyle();
  220. cellStyle.setFont(font);
  221. cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(strFormat));
  222. cell.setCellStyle(cellStyle);
  223. return true;
  224. }
  225. public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
  226. HSSFFont font, short sTop, short sBottom, short sLeft, short sRight) {
  227. if (cell == null)
  228. return false;
  229. HSSFCellStyle cellStyle = wb.createCellStyle();
  230. cellStyle.setFont(font);
  231. setBordersLine(cellStyle, sTop, sBottom, sLeft, sRight);
  232. cell.setCellStyle(cellStyle);
  233. return true;
  234. }
  235. public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
  236. HSSFFont font, String strFormat, short sTop, short sBottom,
  237. short sLeft, short sRight) {
  238. if (cell == null)
  239. return false;
  240. HSSFCellStyle cellStyle = wb.createCellStyle();
  241. cellStyle.setFont(font);
  242. cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(strFormat));
  243. setBordersLine(cellStyle, sTop, sBottom, sLeft, sRight);
  244. cell.setCellStyle(cellStyle);
  245. return true;
  246. }
  247. public static boolean setCellStyle(HSSFCell cell, HSSFWorkbook wb,
  248. short sTop, short sBottom, short sLeft, short sRight) {
  249. if (cell == null)
  250. return false;
  251. HSSFCellStyle cellStyle = wb.createCellStyle();
  252. setBordersLine(cellStyle, sTop, sBottom, sLeft, sRight);
  253. cell.setCellStyle(cellStyle);
  254. return true;
  255. }
  256. public static void setCellFormat(HSSFCellStyle style, String strFormat) {
  257. if (style == null)
  258. return;
  259. style.setDataFormat(HSSFDataFormat.getBuiltinFormat(strFormat));
  260. }
  261. public static void setBordersColor(HSSFCellStyle style, short sTop,
  262. short sBottom, short sLeft, short sRight) {
  263. if (style == null)
  264. return;
  265. style.setBottomBorderColor(sBottom);
  266. style.setLeftBorderColor(sLeft);
  267. style.setRightBorderColor(sRight);
  268. style.setTopBorderColor(sTop);
  269. }
  270. public static void setBordersLine(HSSFCellStyle style, short sTop,
  271. short sBottom, short sLeft, short sRight) {
  272. if (style == null)
  273. return;
  274. style.setBorderBottom(sBottom);
  275. style.setBorderLeft(sLeft);
  276. style.setBorderRight(sRight);
  277. style.setBorderTop(sTop);
  278. }
  279. public static void setCellFont(HSSFCellStyle style, HSSFFont font) {
  280. if (style == null)
  281. return;
  282. style.setFont(font);
  283. }
  284. public static void setCellAlign(HSSFCell cell, short halign, short valign) {
  285. HSSFCellStyle cellStyle = cell.getCellStyle();
  286. cellStyle.setAlignment(halign);
  287. cellStyle.setVerticalAlignment(valign);
  288. cell.setCellStyle(cellStyle);
  289. }
  290. public static void setCellBkGround(HSSFCell cell, short sBkGroud) {
  291. HSSFCellStyle cellStyle = cell.getCellStyle();
  292. cellStyle.setFillForegroundColor(sBkGroud);
  293. cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  294. }
  295. public static void setCellBkGround(HSSFSheet sheet, int x, int y,
  296. short sBkGroud) {
  297. HSSFRow row = sheet.getRow(x - 1);
  298. if (row == null)
  299. row = sheet.createRow(x - 1);
  300. HSSFCell cell = row.getCell(y - 1);
  301. if (cell == null)
  302. cell = row.createCell(y - 1);
  303. HSSFCellStyle cellStyle = cell.getCellStyle();
  304. cellStyle.setFillForegroundColor(sBkGroud);
  305. cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  306. }
  307. public static void enableWraptext(HSSFCell cell) {
  308. HSSFCellStyle cellStyle = cell.getCellStyle();
  309. cellStyle.setWrapText(true);
  310. cell.setCellStyle(cellStyle);
  311. }
  312. public static void disableWraptext(HSSFCell cell) {
  313. HSSFCellStyle cellStyle = cell.getCellStyle();
  314. cellStyle.setWrapText(false);
  315. cell.setCellStyle(cellStyle);
  316. }
  317. public static void copyRows(HSSFSheet from, int fromRowStart,
  318. int fromRowEnd, HSSFSheet to, int toRowStart) {
  319. int startIndex = fromRowStart - 1;
  320. int endIndex = fromRowEnd - 1;
  321. int toIndex = toRowStart - 1;
  322. for (int index = startIndex; index <= endIndex; index++) {
  323. HSSFRow fromRow = from.getRow(index);
  324. HSSFRow fromTo = to.createRow(toIndex);
  325. copyRow(fromRow, fromTo);
  326. int num = from.getNumMergedRegions();
  327. for (int mergeIndex = 0; mergeIndex < num; mergeIndex++) {
  328. CellRangeAddress r = from.getMergedRegion(mergeIndex);
  329. if (index == r.getFirstRow()) {
  330. int add = toIndex - r.getFirstRow();
  331. CellRangeAddress r2 = new CellRangeAddress(r.getFirstRow()
  332. + add, r.getFirstColumn(), r.getLastRow() + add,
  333. r.getLastColumn());
  334. to.addMergedRegion(r2);
  335. }
  336. }
  337. toIndex++;
  338. }
  339. }
  340. public static void copyRow(HSSFRow from, HSSFRow to) {
  341. // set Row Height
  342. to.setHeight(from.getHeight());
  343. // set cell
  344. for (int i = 0; i < from.getLastCellNum() && i <= 255; i++) {
  345. HSSFCell cell = from.getCell(i);
  346. if (cell == null)
  347. continue;
  348. HSSFCell cell1 = to.createCell(i);
  349. cell1.setCellType(cell.getCellType());
  350. // cell1.setCellFormula(cell.getCellFormula());
  351. // cell1.setCellValue(cell.getStringCellValue());
  352. cell1.setCellStyle(cell.getCellStyle());
  353. if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
  354. cell1.setCellValue(cell.getNumericCellValue());
  355. else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
  356. HSSFRichTextString str = cell.getRichStringCellValue();
  357. if (str.length() > 0) {
  358. cell1.setCellValue(str);
  359. }
  360. } else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA)
  361. cell1.setCellFormula(cell.getCellFormula());
  362. }
  363. }
  364. public static void createimage(byte[] image, String strPicUrl) {
  365. try {
  366. FileOutputStream out = new FileOutputStream(strPicUrl);
  367. out.write(image);
  368. out.close();
  369. } catch (Exception e) {
  370. e.printStackTrace();
  371. }
  372. }
  373. public static void deletefile(String path) {
  374. try {
  375. File f = new File(path);
  376. if (f.exists())
  377. f.delete();
  378. } catch (Exception e) {
  379. e.printStackTrace();
  380. }
  381. }
  382. public static void setRowHeight(HSSFSheet sheet, int nRow, float fHeight) {
  383. HSSFRow row = sheet.getRow(nRow - 1);
  384. if (row == null)
  385. row = sheet.createRow(nRow - 1);
  386. row.setHeightInPoints(fHeight);
  387. }
  388. public static void setFooter(HSSFSheet sheet, String strFontName,
  389. String strFooter) {
  390. String strSplit = " ";
  391. HSSFFooter footer = sheet.getFooter();
  392. footer.setCenter(HSSFFooter.font(strFontName, "Normal")
  393. + HSSFFooter.fontSize((short) 8) + strSplit + strFooter);
  394. }
  395. public static void setRegionBorders(HSSFWorkbook wb, HSSFSheet sheet,
  396. CellRangeAddress region) {
  397. if (region == null)
  398. return;
  399. for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
  400. HSSFRow row = sheet.getRow(i);
  401. if (row == null)
  402. row = sheet.createRow(i);
  403. for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
  404. HSSFCell cell = row.getCell(j);
  405. if (cell == null) {
  406. cell = row.createCell(j);
  407. }
  408. setCellStyle(cell, wb, HSSFCellStyle.BORDER_THIN,
  409. HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN,
  410. HSSFCellStyle.BORDER_THIN);
  411. }
  412. }
  413. }
  414. public static void main(String[] arg) {
  415. try {
  416. // FileInputStream fIn = new FileInputStream("d:\\test.xls");
  417. // HSSFWorkbook wb = new HSSFWorkbook(fIn);
  418. // HSSFSheet st = wb.getSheetAt(0);
  419. // HSSFCell cell = getCell(st, 4, 1);
  420. // System.out.println(cell.getCellFormula());
  421. // System.out.println(cell.getNumericCellValue());
  422. // HSSFRow row = st.createRow(18);
  423. // cell = row.createCell(8);
  424. // cell.setCellFormula("SUM(I1:I18)");
  425. // System.out.println(cell.getNumericCellValue());
  426. // cell.setCellValue("=SUM(I1:I18)");
  427. HSSFWorkbook wb = new HSSFWorkbook();
  428. for (int sheetNo = 1; sheetNo < 5; sheetNo++) {
  429. HSSFSheet st = wb.createSheet("TheSheet" + sheetNo);
  430. for (int rowNo = 0; rowNo < 10000; rowNo++) {
  431. HSSFRow row = st.createRow(rowNo);
  432. for (int colNo = 0; colNo < 10; colNo++) {
  433. HSSFCell cell = row.createCell(colNo);
  434. String cellValue = "Sheet:" + sheetNo + " Row:" + rowNo
  435. + " Col:" + colNo;
  436. cell.setCellValue(cellValue);
  437. System.out.println(cellValue);
  438. }
  439. }
  440. }
  441. FileOutputStream fOut;
  442. fOut = new FileOutputStream("d:\\test.xls");
  443. // 将模板的内容写到输出文件上
  444. wb.write(fOut);
  445. fOut.flush();
  446. // 操作结束,关闭文件
  447. fOut.close();
  448. } catch (Exception e) {
  449. e.printStackTrace();
  450. }
  451. }
  452. }