Index: forgon-tools/src/main/java/com/forgon/tools/itextpdf/ExcelToPdf.java =================================================================== diff -u -r25891 -r35510 --- forgon-tools/src/main/java/com/forgon/tools/itextpdf/ExcelToPdf.java (.../ExcelToPdf.java) (revision 25891) +++ forgon-tools/src/main/java/com/forgon/tools/itextpdf/ExcelToPdf.java (.../ExcelToPdf.java) (revision 35510) @@ -7,13 +7,17 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.List; - +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import jxl.Cell; import jxl.Range; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; - +import com.forgon.tools.excel.ParerUtils; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; @@ -32,7 +36,162 @@ * @since 2019-03-08 */ public class ExcelToPdf { + /** + * xlsx转换pdf + * @param excelInputStream + * @param pdfFilePath + * @throws DocumentException + * @throws IOException + */ + public static void xlsxToPdf(InputStream excelInputStream,String pdfFilePath) throws DocumentException, IOException { + Document document = new Document(PageSize.A4, 0, 0, 50, 0); + OutputStream ous = new FileOutputStream(pdfFilePath); + PdfWriter writer = PdfWriter.getInstance(document, ous); + // 字体设置 + + /** + * 由于itext不支持中文,所以需要进行字体的设置,我这里让itext调用windows系统的中文字体, + * 找到文件后,打开属性,将文件名及所在路径作为字体名即可。 + */ + + // 创建BaseFont对象,指明字体,编码方式,是否嵌入 + BaseFont bf = BaseFont.createFont("C:\\Windows\\Fonts\\simkai.ttf", + BaseFont.IDENTITY_H, false); + // 创建Font对象,将基础字体对象,字体大小,字体风格 + Font font = new Font(bf, 13, Font.NORMAL); + int rowNum = 0; + int colNum = 0; + try { + XSSFWorkbook workbook = new XSSFWorkbook(excelInputStream); + XSSFSheet sheet = workbook.getSheetAt(0); + int firstRowNum = sheet.getFirstRowNum(); + int columns = sheet.getRow(firstRowNum).getLastCellNum();//第一行的列数 + int lastRowNum = sheet.getLastRowNum(); + int column = columns; + // 下面是找出表格中的空行和空列 + List nullCol = new ArrayList<>(); + List nullRow = new ArrayList<>(); + for (int j = 0; j < columns; j++) { + int nullColNum = 0; + for (int i = 0; i < lastRowNum; i++) { + XSSFRow row = sheet.getRow(i); + String str = ""; + if(row != null){ + XSSFCell cell = row.getCell(j); + str = ParerUtils.getXSSFCellValue(cell, ""); + } + if (str == null || "".equals(str)) { + nullColNum++; + } + } + if (nullColNum == lastRowNum) { + nullCol.add(j); + column--; + } + } + + for (int i = 0; i < lastRowNum; i++) { + int nullRowNum = 0; + XSSFRow xrow = sheet.getRow(i); + for (int j = 0; j < columns; j++) { + String str = ""; + if(xrow != null){ + XSSFCell cell = xrow.getCell(j); + str = ParerUtils.getXSSFCellValue(cell, ""); + } + if (str == null || "".equals(str)) { + nullRowNum++; + } + } + if (nullRowNum == columns) { + nullRow.add(i); + } + } + PdfPTable table = new PdfPTable(column); + int rangesNum = sheet.getNumMergedRegions(); + PdfPCell cell1 = new PdfPCell(); + for (int i = 0; i < lastRowNum ; i++) { + if (nullRow.contains(i)) { // 如果这一行是空行,这跳过这一行 + continue; + } + XSSFRow row = sheet.getRow(i); + for (int j = 0; j < row.getLastCellNum(); j++) { + if (nullCol.contains(j)) { // 如果这一列是空列,则跳过这一列 + continue; + } + boolean flag = true; + XSSFCell cell = row.getCell(j); + String str = ParerUtils.getXSSFCellValue(cell, ""); + for (int k = 0; k < rangesNum; k++) { + CellRangeAddress range = sheet.getMergedRegion(k); + // 合并的单元格判断和处理 + if (j >= range.getFirstColumn() + && j <= range.getLastColumn() + && i >= range.getFirstRow() + && i <= range.getLastRow()) { + if (str == null || "".equals(str)) { + flag = false; + break; + } + rowNum = range.getLastRow() - range.getFirstRow() + 1; + colNum = range.getLastColumn() - range.getFirstColumn() + 1; + if (rowNum > colNum) { + cell1 = mergeRow(str, font, rowNum); + cell1.setColspan(colNum); + table.addCell(cell1); + } else { + cell1 = mergeCol(str, font, colNum); + cell1.setRowspan(rowNum); + table.addCell(cell1); + } + flag = false; + break; + } + } + if (flag) { + table.addCell(getPDFCell(str, font)); + } + } + } + document.open(); + document.add(table); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + try { + document.close(); + } catch (Exception e2) { + // TODO: handle exception + } + try { + writer.close(); + } catch (Exception e2) { + // TODO: handle exception + } + try { + writer.flush(); + } catch (Exception e2) { + // TODO: handle exception + } + try { + ous.close(); + } catch (Exception e2) { + // TODO: handle exception + } + try { + ous.flush(); + } catch (Exception e2) { + // TODO: handle exception + } + try { + excelInputStream.close(); + } catch (Exception e2) { + // TODO: handle exception + } + } + } /** * excel转换成pdf * Index: forgon-tools/src/main/java/com/forgon/tools/excel/ParerUtils.java =================================================================== diff -u -r22818 -r35510 --- forgon-tools/src/main/java/com/forgon/tools/excel/ParerUtils.java (.../ParerUtils.java) (revision 22818) +++ forgon-tools/src/main/java/com/forgon/tools/excel/ParerUtils.java (.../ParerUtils.java) (revision 35510) @@ -7,14 +7,15 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; - import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.ss.util.NumberToTextConverter; +import org.apache.poi.xssf.usermodel.XSSFCell; import com.forgon.tools.format.FormatNumber; import com.forgon.tools.format.FormatString; @@ -200,7 +201,50 @@ } return s.trim(); } - + /** + * 获取XSSFCell的值 + * 数值类型按原格式返回字符串 + * @param cell + * @param defaultValue + * @return + */ + public static String getXSSFCellValue(XSSFCell cell, String defaultValue) { + String returnCellValue = defaultValue; + if (cell != null) { + if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { + returnCellValue = cell.getRichStringCellValue().getString(); + if(returnCellValue != null){ + returnCellValue = replaceSpecialCharacter(returnCellValue); + } + } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { + if (HSSFDateUtil.isCellDateFormatted(cell)) { + Date date = cell.getDateCellValue(); + if (date != null) { + returnCellValue = new SimpleDateFormat("yyyy-MM-dd") + .format(date); + } + } else { + returnCellValue = NumberToTextConverter.toText(cell.getNumericCellValue()); + } + + } else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) { + double cellValue = cell.getNumericCellValue(); + if (Double.isNaN(cellValue)) { + returnCellValue = "NaN"; + } else { + BigDecimal bg = new BigDecimal(cellValue); + returnCellValue = FormatNumber.formatToString(bg + .doubleValue()); + } + + } + } + + if (returnCellValue != null) { + returnCellValue = returnCellValue.trim(); + } + return returnCellValue; + } public static String getCellValue(HSSFCell cell, String defaultValue) { String returnCellValue = defaultValue; Index: forgon-tools/src/main/java/com/forgon/tools/hibernate/ObjectDaoImpl.java =================================================================== diff -u -r34631 -r35510 --- forgon-tools/src/main/java/com/forgon/tools/hibernate/ObjectDaoImpl.java (.../ObjectDaoImpl.java) (revision 34631) +++ forgon-tools/src/main/java/com/forgon/tools/hibernate/ObjectDaoImpl.java (.../ObjectDaoImpl.java) (revision 35510) @@ -27,7 +27,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; -import org.apache.poi.hssf.record.formula.functions.T; +import org.apache.poi.ss.formula.functions.T; import org.hibernate.CacheMode; import org.hibernate.HibernateException; import org.hibernate.LockMode; @@ -271,7 +271,7 @@ if(CollectionUtils.isEmpty(cols)){ return resultList; } - for(List col : cols){ + for(List col : cols){ if(CollectionUtils.isEmpty(col)){ continue; }