public void bprint_actiontage() {
try {
DateFormat formatdate1 = new SimpleDateFormat("dd/MM/yyyy");
DateFormat formatdate2 = new SimpleDateFormat("yyMMddhhmmss");
//excel路径和文件名称
String pdfFileName = "E:\\build\\web\\resources\\" + formatdate2.format(new Date()) + ".xls";
//插入图片的路径 必须是png格式的图片
String logo = "E:\\build\\web\\resources\\logo.png";
String pringTitel = "打印标题";
//创建excel
WritableWorkbook workbook = Workbook.createWorkbook(new File(pdfFileName));
WritableFont wf12 = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);
WritableFont wf10 = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);
WritableFont wf16 = new WritableFont(WritableFont.createFont("宋体"), 16, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
WritableFont wfb12 = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE); WritableCellFormat flt16 = new WritableCellFormat(wf16);
flt16.setAlignment(jxl.format.Alignment.LEFT);
WritableCellFormat frtbl12 = new WritableCellFormat(wfb12);
frtbl12.setAlignment(jxl.format.Alignment.RIGHT);
WritableCellFormat flt10 = new WritableCellFormat(wf10);
flt10.setAlignment(jxl.format.Alignment.LEFT);
WritableCellFormat flt12 = new WritableCellFormat(wf12);
flt12.setAlignment(jxl.format.Alignment.LEFT);
WritableCellFormat bordelrt = new WritableCellFormat(wf12);
bordelrt.setWrap(true);//是否自动换行
bordelrt.setBorder(jxl.format.Border.LEFT, jxl.format.BorderLineStyle.THIN);//设置边框
bordelrt.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.THIN);
WritableCellFormat bordelrtbot = new WritableCellFormat(wf12);
bordelrtbot.setBorder(jxl.format.Border.LEFT, jxl.format.BorderLineStyle.THIN);
bordelrtbot.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.THIN);
bordelrtbot.setBorder(jxl.format.Border.BOTTOM, jxl.format.BorderLineStyle.THIN);
bordelrtbot.setWrap(true);
WritableCellFormat bordeall = new WritableCellFormat(wf12);
bordeall.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
bordeall.setWrap(true);
bordeall.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//设置对垂直对齐方式
bordeall.setAlignment(jxl.format.Alignment.LEFT);//设置水平对齐方式
WritableSheet sheet = workbook.createSheet("pricetag", 0); // 添加工作表
SheetSettings sheetset = sheet.getSettings();
sheetset.setLeftMargin(Double.parseDouble("0"));
sheetset.setRightMargin(Double.parseDouble("0"));
sheetset.setTopMargin(Double.parseDouble("0.5"));
sheetset.setBottomMargin(Double.parseDouble("0.6"));
sheetset.setFooterMargin(Double.parseDouble("0.2"));//设置边距
sheetset.setShowGridLines(false);//是否显示网格线
sheetset.setPageStart(1);//开始页码
sheetset.setPrintTitlesRow(1, 11);//设置固定打印标题
//设置页脚
HeaderFooter hd = new HeaderFooter();
hd.getRight().append("Page ");
hd.getRight().appendPageNumber();//设置当前页
hd.getRight().append(" of ");
hd.getRight().appendTotalPages();//设置总页数
sheetset.setFooter(hd);//添加页脚
//列, 行,内容
sheet.setColumnView(0, 2); // 设置列的宽度
sheet.setColumnView(1, 3); // 设置列的宽度
sheet.setRowView(0, 260); // 设置行的高度
sheet.setRowView(5, 100); // 设置行的高度
sheet.setColumnView(4, 2); // 设置列的宽度
sheet.setColumnView(5, 6); // 设置列的宽度
sheet.setColumnView(6, 8); // 设置列的宽度
sheet.setColumnView(8, 9); // 设置列的宽度
sheet.setColumnView(9, 9); // 设置列的宽度
sheet.setColumnView(10, 6); // 设置列的宽度
sheet.setColumnView(11, 4); // 设置列的宽度
sheet.setColumnView(12, 4); // 设置列的宽度
sheet.setColumnView(13, 4); // 设置列的宽度
sheet.setColumnView(15, 10); // 设置列的宽度
sheet.setColumnView(16, 1); // 设置列的宽度
//报表头部信息 begin
//列,行,列,行
sheet.mergeCells(1, 1, 2, 4);
sheet.mergeCells(3, 1, 16, 1);
sheet.mergeCells(3, 2, 16, 2);
sheet.mergeCells(3, 3, 7, 3);
sheet.mergeCells(8, 3, 12, 3);
sheet.mergeCells(3, 4, 7, 3);
sheet.mergeCells(8, 4, 12, 3);
sheet.mergeCells(5, 6, 16, 6);
File file = new File(logo); // 获得图片
WritableImage image = new WritableImage(1, 1, 2, 4, file); // 设置图片显示位置 起始坐标,所占单元格。
sheet.addImage(image); // 加载图片
Label label = new Label(3, 1, "XXXXXXXXXXXXXXXXXXXXXXX", flt16);
sheet.addCell(label);
label = new Label(3, 2, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.", flt10);
sheet.addCell(label);
label = new Label(3, 3, "Tel: XXXXXXXXXX", flt12);
sheet.addCell(label);
label = new Label(8, 3, "Fax: XXXXXXXXXX", flt12);
sheet.addCell(label);
label = new Label(3, 4, "XXXXXX XXXXXX", flt12);
sheet.addCell(label);
label = new Label(8, 4, "XXXXXX XXXXXXX", flt12);
sheet.addCell(label);
label = new Label(5, 6, pringTitel, frtbl12);
sheet.addCell(label);
//报表头部信息 end
//报价 总 信息 begin
sheet.mergeCells(1, 7, 8, 7);
sheet.mergeCells(9, 7, 11, 7);
sheet.mergeCells(12, 7, 16, 7);
sheet.mergeCells(1, 8, 8, 8);
sheet.mergeCells(9, 8, 11, 8);
sheet.mergeCells(12, 8, 16, 8);
sheet.mergeCells(1, 9, 16, 9);
sheet.mergeCells(2, 10, 3, 10);
sheet.mergeCells(4, 10, 9, 10);
sheet.mergeCells(11, 10, 13, 10);
sheet.mergeCells(14, 10, 16, 10);
label = new Label(1, 7, "XXXXXXXXX", bordeall);
sheet.addCell(label);
label = new Label(9, 7, "XXXXX", bordeall);
sheet.addCell(label);
label = new Label(12, 7, "XXXXX", bordeall);
sheet.addCell(label);
label = new Label(1, 8, "XXXXX", bordeall);
sheet.addCell(label);
label = new Label(9, 8, "XXXX", bordeall);
sheet.addCell(label);
label = new Label(12, 8, "XXXXXXX" + formatdate1.format(new Date()), bordeall);
sheet.addCell(label);
label = new Label(1, 9, "XXXX", bordeall);
sheet.addCell(label);
label = new Label(1, 10, "XXX", bordeall);
sheet.addCell(label);
label = new Label(2, 10, "XXXXX", bordeall);
sheet.addCell(label);
label = new Label(4, 10, "XXXXXX", bordeall);
sheet.addCell(label);
label = new Label(10, 10, "XXXXX", bordeall);
sheet.addCell(label);
label = new Label(11, 10, "XXXXX", bordeall);
sheet.addCell(label);
label = new Label(14, 10, "XXXXX", bordeall);
sheet.addCell(label);
//报价 总 信息 end
int row = 10;
for (int i = 1; i < 12; i++) {
//第一行
label = new Label(1, row + 1, "", bordelrt);
sheet.addCell(label);
sheet.mergeCells(2, row + 1, 3, row + 1);
label = new Label(2, row + 1, "", bordelrt);
sheet.addCell(label);
sheet.mergeCells(4, row + 1, 9, row + 1);
String cont1 = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
cont1 = cont1.toUpperCase();
if (cont1.length() > 35) {
int d = cont1.length() / 35;
if (cont1.length() % 35 > 0) {
d = d + 1;
}
sheet.setRowView(row + 1, 300 * d); // 设置行的高度
}
label = new Label(4, row + 1, cont1.toUpperCase(), bordelrt);
sheet.addCell(label);
label = new Label(10, row + 1, "", bordelrt);
sheet.addCell(label);
sheet.mergeCells(11, row + 1, 13, row + 1);
label = new Label(11, row + 1, "", bordelrt);
sheet.addCell(label);
image = new WritableImage(14, row + 1, 2, 4, file); // 设置图片显示位置 起始坐标,所占单元格。
sheet.addImage(image); // 加载图片
sheet.mergeCells(16, row + 1, 16, row + 4);
label = new Label(16, row + 1, "", bordeall);
sheet.addCell(label);
//第二行
label = new Label(1, row + 2, String.valueOf(i), bordelrt);
sheet.addCell(label);
sheet.mergeCells(2, row + 2, 3, row + 2);
label = new Label(2, row + 2, "XXXXXXXXXX", bordelrt);
sheet.addCell(label);
sheet.mergeCells(4, row + 2, 9, row + 2);
label = new Label(4, row + 2, "XXXXXXXXX", bordelrt);
sheet.addCell(label);
label = new Label(10, row + 2, "XXXXX", bordelrt);
sheet.addCell(label);
sheet.mergeCells(11, row + 2, 13, row + 2);
label = new Label(11, row + 2, "XXXX", bordelrt);
sheet.addCell(label);
//第三行
label = new Label(1, row + 3, "", bordelrt);
sheet.addCell(label);
sheet.mergeCells(2, row + 3, 3, row + 3);
label = new Label(2, row + 3, "XXXXXXXX", bordelrt);
sheet.addCell(label);
sheet.mergeCells(4, row + 3, 9, row + 3);
label = new Label(4, row + 3, "XXXXXXXXX", bordelrt);
sheet.addCell(label);
label = new Label(10, row + 3, "", bordelrt);
sheet.addCell(label);
sheet.mergeCells(11, row + 3, 13, row + 3);
label = new Label(11, row + 3, "", bordelrt);
sheet.addCell(label);
//第四行
label = new Label(1, row + 4, "", bordelrtbot);
sheet.addCell(label);
sheet.mergeCells(2, row + 4, 3, row + 4);
label = new Label(2, row + 4, "", bordelrtbot);
sheet.addCell(label);
sheet.mergeCells(4, row + 4, 9, row + 4);
label = new Label(4, row + 4, "", bordelrtbot);
sheet.addCell(label);
label = new Label(10, row + 4, "", bordelrtbot);
sheet.addCell(label);
sheet.mergeCells(11, row + 4, 13, row + 4);
label = new Label(11, row + 4, "", bordelrtbot);
sheet.addCell(label);
sheet.setRowView(row + 5, 50); // 设置行的高度
sheet.mergeCells(1, row + 5, 16, row + 5);
label = new Label(1, row + 5, "", bordeall);
sheet.addCell(label);
row = row + 5;
}
//foot信息 begin
row = row + 1;
sheet.setRowView(row, 450); // 设置行的高度
sheet.setRowView(row + 1, 450); // 设置行的高度
sheet.mergeCells(1, row, 9, row + 1);
label = new Label(1, row, "XXXXXXXXXXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXX.", bordeall);
sheet.addCell(label);
sheet.mergeCells(10, row, 13, row);
label = new Label(10, row, "XXXX", bordeall);
sheet.addCell(label);
sheet.mergeCells(14, row, 16, row);
label = new Label(14, row, "XXX", bordeall);
sheet.addCell(label);
sheet.mergeCells(10, row + 1, 13, row + 1);
label = new Label(10, row + 1, "XXXX", bordeall);
sheet.addCell(label);
sheet.mergeCells(14, row + 1, 16, row + 1);
label = new Label(14, row + 1, "XXXXXX", bordeall);
sheet.addCell(label);
//foot信息 end
workbook.write();
workbook.close();
return;
} catch (Exception ex1) {
ex1.printStackTrace();
}
}