在不知内容的情况下获取Excel表的内容并输出到JSP页面

@RequestMapping(value = "/viewContent/{id}", method = RequestMethod.GET)
	public String do_view(@PathVariable("id") int id, ModelMap map, HttpServletResponse response) {
		FileManage fileManage = fileManageService.findOne(id);
		String filePath = fileManage.getFilePath();
		HSSFSheet sheet = null;
		StringBuilder lsb = new StringBuilder();

		try {
			HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(filePath))); // 获得整个Excel
			for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
				sheet = workbook.getSheetAt(sheetIndex);
				if (sheet != null) {
					int firstRowNum = sheet.getFirstRowNum(); // 第一行
					int lastRowNum = sheet.getLastRowNum(); // 最后一行
					lsb.append("");
					lsb.append("");
					for (int rowNum = firstRowNum; rowNum <= lastRowNum; rowNum++) {
						if (sheet.getRow(rowNum) != null) { // 如果行不为空
							HSSFRow row = sheet.getRow(rowNum);
							int firstCellNum = row.getFirstCellNum();// 该行的第一个单元格
							int lastCellNum = row.getLastCellNum();// 该行的最后一个单元格
							int height = (int) (row.getHeight() / 15.625); // 行的高度
							lsb.append("");
							for (int cellNum = firstCellNum; cellNum <= lastCellNum; cellNum++) { // 循环每一个单元格
								HSSFCell cell = row.getCell(cellNum);
								if (cell != null) {
									lsb.append("");
								}
							}
						}
						lsb.append("");
					}
					lsb.append("
" + sheet.getSheetName() + "
" + getCellValue(cell) + "
"); lsb.append("
"); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } map.addAttribute("content", lsb); return "fileManage/view"; } private static Object getCellValue(HSSFCell cell) throws IOException { Object value = ""; if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { value = cell.getRichStringCellValue().toString(); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); value = sdf.format(date); } else { double value_temp = (double) cell.getNumericCellValue(); BigDecimal bd = new BigDecimal(value_temp); BigDecimal bd1 = bd.setScale(3, bd.ROUND_HALF_UP); value = bd1.doubleValue(); } } if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) { value = ""; } return value; } /** * 判断单元格在不在合并单元格范围内,如果是,获取其合并的列数。 * @param sheet 工作表 * @param cellRow 被判断的单元格的行号 * @param cellCol 被判断的单元格的列号 * @return * @throws IOException */ private static int getMergerCellRegionCol(HSSFSheet sheet, int cellRow,int cellCol) throws IOException { int retVal = 0; int sheetMergerCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergerCount; i++) { CellRangeAddress cra = (CellRangeAddress) sheet.getMergedRegion(i); int firstRow = cra.getFirstRow(); // 合并单元格CELL起始行 int firstCol = cra.getFirstColumn(); // 合并单元格CELL起始列 int lastRow = cra.getLastRow(); // 合并单元格CELL结束行 int lastCol = cra.getLastColumn(); // 合并单元格CELL结束列 if (cellRow >= firstRow && cellRow <= lastRow) { // 判断该单元格是否是在合并单元格中 if (cellCol >= firstCol && cellCol <= lastCol) { retVal = lastCol - firstCol+1; // 得到合并的列数 break; } } } return retVal; } /** * 判断单元格是否是合并的单格,如果是,获取其合并的行数。 * @param sheet 表单 * @param cellRow 被判断的单元格的行号 * @param cellCol 被判断的单元格的列号 * @return * @throws IOException */ private static int getMergerCellRegionRow(HSSFSheet sheet, int cellRow,int cellCol) throws IOException { int retVal = 0; int sheetMergerCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergerCount; i++) { CellRangeAddress cra = (CellRangeAddress) sheet.getMergedRegion(i); int firstRow = cra.getFirstRow(); // 合并单元格CELL起始行 int firstCol = cra.getFirstColumn(); // 合并单元格CELL起始列 int lastRow = cra.getLastRow(); // 合并单元格CELL结束行 int lastCol = cra.getLastColumn(); // 合并单元格CELL结束列 if (cellRow >= firstRow && cellRow <= lastRow) { // 判断该单元格是否是在合并单元格中 if (cellCol >= firstCol && cellCol <= lastCol) { retVal = lastRow - firstRow + 1; // 得到合并的行数 break; } } } return retVal; } /** * 单元格背景色转换 * @param hc * @return */ private String convertToStardColor(HSSFColor hc) { StringBuffer sb = new StringBuffer(""); if (hc != null) { int a = HSSFColor.AUTOMATIC.index; int b = hc.getIndex(); if (a == b) { return null; } sb.append("#"); for (int i = 0; i < hc.getTriplet().length; i++) { String str ; String str_tmp = Integer.toHexString(hc.getTriplet()[i]); if (str_tmp != null && str_tmp.length() < 2) { str = "0" + str_tmp; }else { str = str_tmp; } sb.append(str); } } return sb.toString(); } /** * 单元格小平对齐 * @param alignment * @return */ private String convertAlignToHtml(short alignment) { String align = "left"; switch (alignment) { case HSSFCellStyle.ALIGN_LEFT: align = "left"; break; case HSSFCellStyle.ALIGN_CENTER: align = "center"; break; case HSSFCellStyle.ALIGN_RIGHT: align = "right"; break; default: break; } return align; } /** * 单元格垂直对齐 * @param verticalAlignment * @return */ private String convertVerticalAlignToHtml(short verticalAlignment) { String valign = "middle"; switch (verticalAlignment) { case HSSFCellStyle.VERTICAL_BOTTOM: valign = "bottom"; break; case HSSFCellStyle.VERTICAL_CENTER: valign = "center"; break; case HSSFCellStyle.VERTICAL_TOP: valign = "top"; break; default: break; } return valign; }

你可能感兴趣的:(Java,jsp与js)