Excel PDF预览 excel导出

    //生成条形码
    public File generateFile(String msg) {
        String path = "barcode.png";
        File file = new File(path);
        Code39Bean bean = new Code39Bean();
        final int dpi = 150;
//        // module宽度
        final double moduleWidth = UnitConv.in2mm(1.0f / dpi);
//
//        // 配置对象
        bean.setModuleWidth(moduleWidth);
        bean.setWideFactor(3);
        bean.doQuietZone(false);
        bean.setBarHeight(4);

        String format = "image/png";

        try {

            // 输出到流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(new FileOutputStream(file), format, dpi,
                    BufferedImage.TYPE_BYTE_BINARY, false, 0);

            // 生成条形码
            bean.generateBarcode(canvas, msg);

            // 结束绘制
            canvas.finish();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return file;
    }

  


{ if(state==""){ state="yl";//yl dc } String ProductionOrderNumber="1"; String[] split = ProductionOrderNumber.trim().split(","); OutputStream os = null; OutputStream stream=null; try { HSSFWorkbook workbook = new HSSFWorkbook(); for (String string : split) { //-- HSSFSheet sheet = workbook.createSheet(); sheet.setMargin(HSSFSheet.TopMargin,( double )0 );//调整打印边距 //sheet.setMargin(HSSFSheet.LeftMargin,( double ) 0.1 ); sheet.setMargin(HSSFSheet.BottomMargin,( double ) 0 );//调整打印边距 sheet.setMargin(HSSFSheet.RightMargin,( double ) 0.001 ); // 合并单元格 // CellRangeAddress region = new CellRangeAddress(3, 5, 0, 0); // // sheet.addMergedRegion(region); // region = new CellRangeAddress(0, 0, 1, 3); sheet.setColumnWidth(0, 12 * 256);// 设置列宽 sheet.setColumnWidth(1, 20 * 256);// 设置列宽 sheet.setColumnWidth(2, 25 * 256);// 设置列宽 sheet.setColumnWidth(4, 5 * 256);// 设置列宽 sheet.setColumnWidth(3, 30 * 256);// 设置列宽 sheet.setColumnWidth(5, 7 * 256);// 设置列宽 // 字体样式0 HSSFFont fonts = workbook.createFont(); fonts.setFontHeightInPoints((short) 30);// 字体大小 fonts.setFontName("黑体");// 字体格式 // fonts.setBold(true);// 加粗 // 字体样式1 HSSFFont fonts1 = workbook.createFont(); fonts1.setFontHeightInPoints((short) 30);// 字体大小 fonts1.setFontName("黑体");// 字体格式 // fonts1.setBold(true);// 加粗 HSSFFont fonts2 = workbook.createFont(); fonts2.setFontHeightInPoints((short) 24);// 字体大小 fonts2.setFontName("黑体");// 字体格式 // 样式s HSSFCellStyle styles = workbook.createCellStyle(); styles.setAlignment(HorizontalAlignment.LEFT); styles.setVerticalAlignment(VerticalAlignment.TOP); styles.setBorderLeft(BorderStyle.DOTTED); styles.setBorderBottom(BorderStyle.DOTTED); styles.setWrapText(true); styles.setFont(fonts1); // 样式 new s HSSFCellStyle newstyles = workbook.createCellStyle(); newstyles.setAlignment(HorizontalAlignment.LEFT); newstyles.setVerticalAlignment(VerticalAlignment.TOP); newstyles.setBorderLeft(BorderStyle.DOTTED); newstyles.setBorderBottom(BorderStyle.DOTTED); newstyles.setWrapText(true); newstyles.setFont(fonts2); // 样式0 HSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HorizontalAlignment.LEFT); style.setVerticalAlignment(VerticalAlignment.TOP); style.setBorderLeft(BorderStyle.DOTTED); style.setRotation((short) -90); style.setFont(fonts); // 样式1 HSSFCellStyle style1 = workbook.createCellStyle(); style1.setAlignment(HorizontalAlignment.RIGHT); style1.setVerticalAlignment(VerticalAlignment.TOP); style1.setRotation((short) -90); style1.setFont(fonts); // 样式2 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setAlignment(HorizontalAlignment.LEFT); style2.setVerticalAlignment(VerticalAlignment.BOTTOM); style2.setBorderLeft(BorderStyle.DOTTED); style2.setBorderBottom(BorderStyle.DOTTED); style2.setWrapText(true); style2.setFont(fonts1); // 样式3 HSSFCellStyle style3 = workbook.createCellStyle(); style3.setAlignment(HorizontalAlignment.LEFT); style3.setVerticalAlignment(VerticalAlignment.TOP); style3.setBorderBottom(BorderStyle.DOTTED); style3.setFont(fonts); // 样式4 HSSFCellStyle style4 = workbook.createCellStyle(); style4.setAlignment(HorizontalAlignment.LEFT); style4.setVerticalAlignment(VerticalAlignment.TOP); style4.setBorderLeft(BorderStyle.DOTTED); style4.setFont(fonts); HSSFRow row; HSSFCell cellHeader; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); row = sheet.createRow(0); row.setHeightInPoints(40);// 设置行高 cellHeader = row.createCell(0); cellHeader.setCellValue("kh"); cellHeader.setCellStyle(style3); cellHeader = row.createCell(1); cellHeader.setCellValue("cm.getCustomercode"); cellHeader.setCellStyle(style2); cellHeader = row.createCell(2); cellHeader.setCellStyle(style3); cellHeader = row.createCell(3); cellHeader.setCellStyle(style3); cellHeader = row.createCell(4); cellHeader.setCellValue(" cq:"+sdf.format(new Date())); cellHeader.setCellStyle(style); row = sheet.createRow(1); row.setHeightInPoints(40);// 设置行高 cellHeader = row.createCell(0); cellHeader.setCellValue("xh"); cellHeader.setCellStyle(style3); cellHeader = row.createCell(1); // if(!StringUtils.isEmpty(cm)) { cellHeader.setCellValue("Cyitemno()"); // } cellHeader.setCellStyle(style2); cellHeader = row.createCell(2); cellHeader.setCellStyle(style3); cellHeader = row.createCell(3); cellHeader.setCellStyle(style3); cellHeader = row.createCell(4); cellHeader.setCellStyle(style4); row = sheet.createRow(2); row.setHeightInPoints(40);// 设置行高 cellHeader = row.createCell(0); cellHeader.setCellValue("kx"); cellHeader.setCellStyle(style3); cellHeader = row.createCell(1); cellHeader.setCellStyle(style2); cellHeader = row.createCell(2); cellHeader.setCellStyle(style3); cellHeader = row.createCell(3); cellHeader.setCellStyle(style3); cellHeader = row.createCell(4); cellHeader.setCellStyle(style4); row = sheet.createRow(3); row.setHeightInPoints(160); cellHeader = row.createCell(0); cellHeader.setCellValue(" zs。"); cellHeader.setCellStyle(style1); cellHeader = row.createCell(1); cellHeader.setCellValue(""); cellHeader.setCellStyle(styles); cellHeader = row.createCell(4); cellHeader.setCellStyle(style4); for(int i=4;i<10;i++) { row = sheet.createRow(i); if(i==6) { row.setHeightInPoints(220); cellHeader = row.createCell(0); cellHeader.setCellValue("yw:"); cellHeader.setCellStyle(style1); } cellHeader = row.createCell(1); cellHeader.setCellStyle(style4); cellHeader = row.createCell(4); cellHeader.setCellStyle(style4); } row = sheet.createRow(10); row.setHeightInPoints(30);// 设置行高 cellHeader = row.createCell(0); cellHeader.setCellStyle(style3); cellHeader = row.createCell(1); cellHeader.setCellStyle(style2); cellHeader = row.createCell(2); cellHeader.setCellStyle(style3); cellHeader = row.createCell(5); cellHeader.setCellStyle(style3); row = sheet.createRow(11); row.setHeightInPoints(40);// 设置行高 cellHeader = row.createCell(1); cellHeader.setCellStyle(style4); row = sheet.createRow(12); row.setHeightInPoints(100);// 设置行高 row = sheet.createRow(13); row.setHeightInPoints(25);// 设置行高 ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); BufferedImage bufferImg = ImageIO.read(new File("D:\IMG\2.png")); ImageIO.write(bufferImg, "png", byteArrayOut); HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 3, 10, (short) 5, 14); patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); byteArrayOut = new ByteArrayOutputStream(); bufferImg = ImageIO.read(generateFile("28257")); ImageIO.write(bufferImg, "png", byteArrayOut); patriarch = sheet.createDrawingPatriarch(); anchor = new HSSFClientAnchor(400, 0, 800, 0, (short) 0, 12, (short) 2, 13); patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); } os = response.getOutputStream(); if("dc".equals(state)) { String fileName = "dp.xls"; fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); response.reset(); response.setHeader("Content-Disposition", "attachment;filename="" + fileName + """); response.setContentType("text/html; charset=UTF-8"); workbook.write(os); }else if("yl".equals(state)){ //除去pdf水印 InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); File file1 = new File("D:\sop"); if (!file1.exists()) {// 目录不存在就创建 file1.mkdirs(); } stream = new FileOutputStream(new File(file1, "tag.xls")); workbook.write(stream); File file = new File("D:\sop\tag.pdf"); //新建一个空白pdf文档 if (file.exists()) { file.delete(); } Workbook wb = new Workbook("D:\sop\tag.xls");// 原始excel路径 FileOutputStream fileOS = new FileOutputStream(file); wb.save(fileOS, SaveFormat.PDF); response.setContentType("text/html; charset=UTF-8"); response.setContentType("application/pdf"); FileInputStream bis = new FileInputStream(file.getPath()); int count = 0; byte[] buffer = new byte[1024 * 1024]; while ((count = bis.read(buffer)) != -1) { os.write(buffer, 0, count); } os.flush(); stream.close(); } } catch (Exception e) { e.printStackTrace(); } }

  

其中 

<dependency>
<groupId>com.aspose.cells</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
这个maven仓库 没有 无法下载

jar 我放上面了 这个永久有效

链接:https://pan.baidu.com/s/1eR4yFmJw1K6KOExU0TUq9g
提取码:0o0q

还有一个去水印文件

<?xml version="1.0"?>
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

  





原文地址:https://www.cnblogs.com/Mr-Y1907/p/14325536.html