基于之前的版本,现在给excel中添加条形二维码
excel 数据填充通过对象传递过来。
效果:
后续开发:将条形码的时间戳数据关联到报销单数据中。
提出的需求:
1:需要对报销单进行修改
2:前端项目开发一个扫码功能,扫描条形码实现报销单数据的唯一性。
(用户可能会填写好几个报销单,有可能填写错了,这有导致数据库的错误数据比较多。
扫描条形码实现对真是的报销单进行报销,修改其报销单的有效状态。那么没有状态的报销单则可能是错误数据/前端多次提交错误等。)
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.icsv.www.data.entitys.Reimbursement;
import com.icsv.www.data.entitys.ReimbursementPurpose;
import com.icsv.www.util.string.StringUtils;
/**
* excel工具
* @author think
*/
public class ExcelUtils {
/**
* 报销单excel模板
*/
public static String reimbursementExcel(List<Reimbursement> listR) throws Exception {
// 当前时间戳 需要统一值,其他地方需要用到
String currTimeMillis = System.currentTimeMillis() + "";
// 这是一个下载链接
String excelUploadUrl = "";
Reimbursement r1 = listR.get(0);
Reimbursement r2 = listR.size() == 2 ? listR.get(1) : null;
// 创建工作薄对象
HSSFWorkbook wb = new HSSFWorkbook();
// 创建工作表对象
HSSFSheet sheet = wb.createSheet();
// A4 纸 上半部分
String p1 = "0.00",p2 = "0.00",p3 = "0.00",p4 = "0.00",p5 = "0.00";
// 设置列的宽度
// 设置列宽度
sheet.setColumnWidth(0, 256 * 6);
sheet.setColumnWidth(1, 256 * 6);
sheet.setColumnWidth(2, 256 * 3);
sheet.setColumnWidth(3, 256 * 3);
sheet.setColumnWidth(4, 256 * 3);
sheet.setColumnWidth(5, 256 * 3);
sheet.setColumnWidth(6, 256 * 3);
sheet.setColumnWidth(7, 256 * 3);
sheet.setColumnWidth(8, 256 * 3);
sheet.setColumnWidth(9, 256 * 3);
sheet.setColumnWidth(10, 256 * 3);
sheet.setColumnWidth(11, 256 * 3);
sheet.setColumnWidth(12, 256 * 3);
sheet.setColumnWidth(13, 256 * 3);
sheet.setColumnWidth(14, 256 * 3);
sheet.setColumnWidth(15, 256 * 3);
sheet.setColumnWidth(16, 256 * 3);
sheet.setColumnWidth(17, 256 * 3);
sheet.setColumnWidth(18, 256 * 3);
sheet.setColumnWidth(19, 256 * 3);
sheet.setColumnWidth(20, 256 * 4);
sheet.setColumnWidth(21, 256 * 4);
sheet.setColumnWidth(22, 256 * 4);
sheet.setColumnWidth(23, 256 * 3);
sheet.setColumnWidth(24, 256 * 3);
sheet.setColumnWidth(25, 256 * 4);
sheet.setColumnWidth(26, 256 * 4);
sheet.setColumnWidth(27, 256 * 4);
// 设置该行高度
HSSFRow row = null;
// 第一张报销单
row = sheet.createRow(0);
row.setHeight((short) 600);
row = sheet.createRow(1);
row.setHeight((short) 50);
row = sheet.createRow(2);
row.setHeight((short) 200);
row = sheet.createRow(3);
row.setHeight((short) 600);
row = sheet.createRow(4);
row.setHeight((short) 300);
row = sheet.createRow(5);
row.setHeight((short) 300);
row = sheet.createRow(6);
row.setHeight((short) 600);
row = sheet.createRow(7);
row.setHeight((short) 600);
row = sheet.createRow(8);
row.setHeight((short) 600);
row = sheet.createRow(9);
row.setHeight((short) 600);
row = sheet.createRow(10);
row.setHeight((short) 600);
row = sheet.createRow(11);
row.setHeight((short) 600);
row = sheet.createRow(12);
row.setHeight((short) 600);
row = sheet.createRow(13);
row.setHeight((short) 600);
// 第十六行 折叠中间线
row = sheet.createRow(14);
row.setHeight((short) 600);
row = sheet.createRow(15);
row.setHeight((short) 400);
row = sheet.createRow(16);
row.setHeight((short) 600);
// 第二张报销单
row = sheet.createRow(17);
row.setHeight((short) 600);
row = sheet.createRow(18);
row.setHeight((short) 50);
row = sheet.createRow(19);
row.setHeight((short) 200);
row = sheet.createRow(20);
row.setHeight((short) 600);
row = sheet.createRow(21);
row.setHeight((short) 300);
row = sheet.createRow(22);
row.setHeight((short) 300);
row = sheet.createRow(23);
row.setHeight((short) 600);
row = sheet.createRow(24);
row.setHeight((short) 600);
row = sheet.createRow(25);
row.setHeight((short) 600);
row = sheet.createRow(26);
row.setHeight((short) 600);
row = sheet.createRow(27);
row.setHeight((short) 600);
row = sheet.createRow(28);
row.setHeight((short) 600);
row = sheet.createRow(29);
row.setHeight((short) 600);
row = sheet.createRow(30);
row.setHeight((short) 600);
row = sheet.createRow(30);
row.setHeight((short) 600);
HSSFCell cell = row.createCell(0);
// 第一行
// 合并第一行 从第一行开始第一行结束 第零列开始 第二十七列结束
CellRangeAddress cellRangeAddress42 = new CellRangeAddress(0, 0, 0, 10);
sheet.addMergedRegion(cellRangeAddress42);
CellRangeAddress cellRangeAddress40 = new CellRangeAddress(0, 0, 11, 27);
sheet.addMergedRegion(cellRangeAddress40);
row = sheet.createRow(0);
row.setHeight((short) 600);
BufferedImage bufferImg = null;
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
// 获取条形码路径
// String imageUrl = GoogleBarCodeUtils.getCode("id=excel_1", currTimeMillis);
// 线二维码条形路径
String imageUrl = "http://xxxx.xxx.com:port/1657524231692.png";
String names = imageUrl.substring(imageUrl.lastIndexOf("/")+1);
//获取图片后缀
String imageType = imageUrl.substring(imageUrl.length()-3);
// 这是存放本地路径
File imageFile = new File("C://image//code//" + names);
FileUtils.copyURLToFile(new URL(imageUrl), imageFile);
bufferImg = ImageIO.read(imageFile);
ImageIO.write(bufferImg, imageType, byteArrayOut);
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 0, (short) 6, 1);
anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
// cell = row.createCell(0);
// cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size20.getValue(),
// HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
// cell.setCellValue(new HSSFRichTextString("费用报销单"));
cell = row.createCell(11);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size20.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, true, false, false));
cell.setCellValue(new HSSFRichTextString("费用报销单"));
// 第二行
CellRangeAddress cellRangeAddress41 = new CellRangeAddress(1, 1, 10, 17);
sheet.addMergedRegion(cellRangeAddress41);
// 第三行
// 报销部门
CellRangeAddress cellRangeAddress33 = new CellRangeAddress(3, 3, 0, 9);
sheet.addMergedRegion(cellRangeAddress33);
// 填写区域(报销部门)
// CellRangeAddress cellRangeAddress34 = new CellRangeAddress(3, 3, 3, 10);
// sheet.addMergedRegion(cellRangeAddress34);
// 填写区域(年份)
CellRangeAddress cellRangeAddress35 = new CellRangeAddress(3, 3, 10, 11);
sheet.addMergedRegion(cellRangeAddress35);
// 填写区域(月份)
CellRangeAddress cellRangeAddress36 = new CellRangeAddress(3, 3, 13, 13);
sheet.addMergedRegion(cellRangeAddress36);
// 填写区域(日)
CellRangeAddress cellRangeAddress37 = new CellRangeAddress(3, 3, 14, 14);
sheet.addMergedRegion(cellRangeAddress37);
// 日填
CellRangeAddress cellRangeAddress38 = new CellRangeAddress(3, 3, 16, 17);
sheet.addMergedRegion(cellRangeAddress38);
// 单据及附件共
CellRangeAddress cellRangeAddress39 = new CellRangeAddress(3, 3, 20, 25);
sheet.addMergedRegion(cellRangeAddress39);
row = sheet.createRow(3);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("报销部门:${depart}"));
cell.setCellValue(new HSSFRichTextString(cell.getRichStringCellValue().toString().replace("${depart}", r1.getDepartment())));
// 这是填写的值
cell = row.createCell(10);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString(r1.getApplyTime().split("-")[0]));
cell = row.createCell(12);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("年"));
// 这是填写的值
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString(r1.getApplyTime().split("-")[1]));
cell = row.createCell(14);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("月"));
// 这是填写的值
cell = row.createCell(15);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString(r1.getApplyTime().split("-")[2]));
cell = row.createCell(16);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("日填"));
cell = row.createCell(20);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("附件共"));
// 填写的页码值
cell = row.createCell(26);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, true, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString(r1.getBillNum()));
cell = row.createCell(27);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("页"));
// 第四行
// 发生日期
CellRangeAddress cellRangeAddress1 = new CellRangeAddress(4, 4, 0, 1);
sheet.addMergedRegion(cellRangeAddress1);
// 用途
CellRangeAddress cellRangeAddress2 = new CellRangeAddress(4, 5, 2, 12);
sheet.addMergedRegion(cellRangeAddress2);
// 金额
CellRangeAddress cellRangeAddress3 = new CellRangeAddress(4, 5, 13, 17);
sheet.addMergedRegion(cellRangeAddress3);
// 备注
CellRangeAddress cellRangeAddress4 = new CellRangeAddress(4, 8, 18, 19);
sheet.addMergedRegion(cellRangeAddress4);
// 签字区域
CellRangeAddress cellRangeAddress32 = new CellRangeAddress(4, 8, 20, 27);
sheet.addMergedRegion(cellRangeAddress32);
row = sheet.createRow(4);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("发生日期"));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("用 途"));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("金额(元)"));
cell = row.createCell(18);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, true, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("备 注"));
// 这里填写备注的内容
cell = row.createCell(20);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, true));
cell.setCellValue(new HSSFRichTextString(r1.getRemarks()));
// 第五行
CellRangeAddress cellRangeAddress6 = new CellRangeAddress(5, 5, 2, 12);
sheet.addMergedRegion(cellRangeAddress6);
CellRangeAddress cellRangeAddress7 = new CellRangeAddress(5, 5, 13, 17);
sheet.addMergedRegion(cellRangeAddress7);
row = sheet.createRow(5);
row.setHeight((short) 300);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, false, true, true, false));
cell.setCellValue(new HSSFRichTextString("月"));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, false, true, true, false));
cell.setCellValue(new HSSFRichTextString("日"));
// 第六行
CellRangeAddress cellRangeAddress8 = new CellRangeAddress(6, 6, 2, 12);
sheet.addMergedRegion(cellRangeAddress8);
CellRangeAddress cellRangeAddress9 = new CellRangeAddress(6, 6, 13, 17);
sheet.addMergedRegion(cellRangeAddress9);
row = sheet.createRow(6);
row.setHeight((short) 600);
List<ReimbursementPurpose> listRP1 = r1.getListRP();
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 0 ? listRP1.get(0).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 0 ? listRP1.get(0).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 0 ? listRP1.get(0).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP1.size() > 0 )
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP1.get(0).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP1.get(0).getPrice())));
p1 = cell.getRichStringCellValue().toString();
// 第七行
CellRangeAddress cellRangeAddress10 = new CellRangeAddress(7, 7, 2, 12);
sheet.addMergedRegion(cellRangeAddress10);
CellRangeAddress cellRangeAddress11 = new CellRangeAddress(7, 7, 13, 17);
sheet.addMergedRegion(cellRangeAddress11);
row = sheet.createRow(7);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 1 ? listRP1.get(1).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 1 ? listRP1.get(1).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 1 ? listRP1.get(1).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP1.size() > 1)
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP1.get(1).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP1.get(1).getPrice())));
p2 = cell.getRichStringCellValue().toString();
// 第八行
CellRangeAddress cellRangeAddress12 = new CellRangeAddress(8, 8, 2, 12);
sheet.addMergedRegion(cellRangeAddress12);
CellRangeAddress cellRangeAddress13 = new CellRangeAddress(8, 8, 13, 17);
sheet.addMergedRegion(cellRangeAddress13);
row = sheet.createRow(8);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 2 ? listRP1.get(2).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 2 ? listRP1.get(2).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 2 ? listRP1.get(2).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP1.size() > 2)
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP1.get(2).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP1.get(2).getPrice())));
p3 = cell.getRichStringCellValue().toString();
// 第九行
CellRangeAddress cellRangeAddress14 = new CellRangeAddress(9, 9, 2, 12);
sheet.addMergedRegion(cellRangeAddress14);
CellRangeAddress cellRangeAddress15 = new CellRangeAddress(9, 9, 13, 17);
sheet.addMergedRegion(cellRangeAddress15);
// 部门审核
CellRangeAddress cellRangeAddress5 = new CellRangeAddress(9, 11, 18, 19);
sheet.addMergedRegion(cellRangeAddress5);
// 签字区域
CellRangeAddress cellRangeAddress29 = new CellRangeAddress(9, 11, 20, 22);
sheet.addMergedRegion(cellRangeAddress29);
// 领导审批
CellRangeAddress cellRangeAddress30 = new CellRangeAddress(9, 11, 23, 24);
sheet.addMergedRegion(cellRangeAddress30);
// 签字区域
CellRangeAddress cellRangeAddress31 = new CellRangeAddress(9, 11, 25, 27);
sheet.addMergedRegion(cellRangeAddress31);
row = sheet.createRow(9);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 3 ? listRP1.get(3).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 3 ? listRP1.get(3).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 3 ? listRP1.get(3).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP1.size() > 3)
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP1.get(3).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP1.get(3).getPrice())));
p4 = cell.getRichStringCellValue().toString();
cell = row.createCell(18);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, true, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("部门审核"));
cell = row.createCell(23);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, true, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("领导审批"));
// 第十行
CellRangeAddress cellRangeAddress16 = new CellRangeAddress(10, 10, 2, 12);
sheet.addMergedRegion(cellRangeAddress16);
CellRangeAddress cellRangeAddress17 = new CellRangeAddress(10, 10, 13, 17);
sheet.addMergedRegion(cellRangeAddress17);
row = sheet.createRow(10);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 4 ? listRP1.get(4).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 4 ? listRP1.get(4).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP1.size() > 4 ? listRP1.get(4).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP1.size() > 4)
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP1.get(4).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP1.get(4).getPrice())));
p5 = cell.getRichStringCellValue().toString();
// 第十一行
CellRangeAddress cellRangeAddress18 = new CellRangeAddress(11, 11, 0, 12);
sheet.addMergedRegion(cellRangeAddress18);
CellRangeAddress cellRangeAddress19 = new CellRangeAddress(11, 11, 13, 17);
sheet.addMergedRegion(cellRangeAddress19);
row = sheet.createRow(11);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("合 计"));
// 计算合计
String p_all = "0.00";
p_all = priceUtils(p1, p2, "+");
p_all = priceUtils(p_all, p3, "+");
p_all = priceUtils(p_all, p4, "+");
p_all = priceUtils(p_all, p5, "+");
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, false, false, false));
// cell.setCellValue(new HSSFRichTextString("¥:" + p_all));
cell.setCellValue(new HSSFRichTextString(formatTosepara(p_all.replace(",", ""))));
// cell = row.createCell(14);
// cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
// HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, false, false, false));
// cell.setCellValue(new HSSFRichTextString(p_all));
// 第十二行
CellRangeAddress cellRangeAddress20 = new CellRangeAddress(12, 12, 0, 1);
sheet.addMergedRegion(cellRangeAddress20);
row = sheet.createRow(12);
row.setHeight((short) 600);
List<String> price = getCharacters(cell.getRichStringCellValue().toString());
// 金额大写: ${7} 拾 ${6} 万 ${5} 仟 ${4} 佰 ${3} 拾 ${2} 元 ${1} 角 ${0} 分
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("金额大写:"));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price.size() > 7 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price.size() > 7 ? price.get(price.size() - 8) : "⊗"));
cell = row.createCell(3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("拾"));
cell = row.createCell(4);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price.size() > 6 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price.size() > 6 ? price.get(price.size() - 7) : "⊗"));
cell = row.createCell(5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("万"));
cell = row.createCell(6);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price.size() > 5 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price.size() > 5 ? price.get(price.size() - 6) : "⊗"));
cell = row.createCell(7);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("仟"));
cell = row.createCell(8);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price.size() > 4 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price.size() > 4 ? price.get(price.size() - 5) : "⊗"));
cell = row.createCell(9);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("佰"));
cell = row.createCell(10);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price.size() > 3 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price.size() > 3 ? price.get(price.size() - 4) : "⊗"));
cell = row.createCell(11);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("拾"));
cell = row.createCell(12);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price.size() > 2 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price.size() > 2 ? price.get(price.size() - 3) : "⊗"));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("元"));
cell = row.createCell(14);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price.size() > 1 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price.size() > 1 ? price.get(price.size() - 2) : "⊗"));
cell = row.createCell(15);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("角"));
cell = row.createCell(16);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price.size() > 0 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price.size() > 0 ? price.get(price.size() - 1) : "⊗"));
cell = row.createCell(17);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, true, false));
cell.setCellValue(new HSSFRichTextString("分"));
// 报销人签章
CellRangeAddress cellRangeAddress21 = new CellRangeAddress(12, 12, 18, 22);
sheet.addMergedRegion(cellRangeAddress21);
cell = row.createCell(18);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, false, true, false));
cell.setCellValue(new HSSFRichTextString("报销人签章:"));
// 签字区域
CellRangeAddress cellRangeAddress22 = new CellRangeAddress(12, 12, 23, 27);
sheet.addMergedRegion(cellRangeAddress22);
// 第十三行
// 会计主管
CellRangeAddress cellRangeAddress23 = new CellRangeAddress(13, 13, 0, 1);
sheet.addMergedRegion(cellRangeAddress23);
// 签字区域
CellRangeAddress cellRangeAddress24 = new CellRangeAddress(13, 13, 2, 8);
sheet.addMergedRegion(cellRangeAddress24);
// 会计
CellRangeAddress cellRangeAddress25 = new CellRangeAddress(13, 13, 9, 11);
sheet.addMergedRegion(cellRangeAddress25);
// 签字区域
CellRangeAddress cellRangeAddress26 = new CellRangeAddress(13, 13, 12, 17);
sheet.addMergedRegion(cellRangeAddress26);
// 出纳
CellRangeAddress cellRangeAddress27 = new CellRangeAddress(13, 13, 18, 20);
sheet.addMergedRegion(cellRangeAddress27);
// 签字区域
CellRangeAddress cellRangeAddress28 = new CellRangeAddress(13, 13, 21, 27);
sheet.addMergedRegion(cellRangeAddress28);
row = sheet.createRow(13);
row.setHeight((short) 600);
// 第一列
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("会计主管:"));
// 第二列
cell = row.createCell(9);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("会计:"));
cell = row.createCell(18);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("出纳:"));
setRegionBorder(1, cellRangeAddress1, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress2, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress3, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress4, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress5, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress6, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress7, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress8, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress9, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress10, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress11, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress12, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress13, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress14, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress15, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress16, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress17, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress18, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress19, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress20, sheet, wb, true, true, true, false);
setRegionBorder(1, cellRangeAddress21, sheet, wb, true, true, false, false);
setRegionBorder(1, cellRangeAddress22, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress29, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress30, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress31, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress32, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress33, sheet, wb, false, true, false, false);
// setRegionBorder(1, cellRangeAddress34, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress35, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress36, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress37, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress38, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress39, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress40, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress41, sheet, wb, true, true, false, false);
// 中间线
// 第十六行
CellRangeAddress cellRangeAddress50 = new CellRangeAddress(14, 14, 0, 27);
sheet.addMergedRegion(cellRangeAddress50);
CellRangeAddress cellRangeAddress51 = new CellRangeAddress(15, 15, 0, 27);
sheet.addMergedRegion(cellRangeAddress51);
CellRangeAddress cellRangeAddress90 = new CellRangeAddress(16, 16, 0, 27);
sheet.addMergedRegion(cellRangeAddress90);
// 这是一条裁剪线
row = sheet.createRow(15);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString(""));
// A4 纸 下半部分
if (!StringUtils.isObjectNull(r2)) {
String p_t_1 = "0.00",p_t_2 = "0.00",p_t_3 = "0.00",p_t_4 = "0.00",p_t_5 = "0.00";
// 第一行
// 合并第一行 从第一行开始第一行结束 第零列开始 第二十七列结束
CellRangeAddress cellRangeAddress52 = new CellRangeAddress(17, 17, 0, 27);
sheet.addMergedRegion(cellRangeAddress52);
row = sheet.createRow(17);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size20.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("费用报销单"));
// 第二行
CellRangeAddress cellRangeAddress53 = new CellRangeAddress(18, 18, 10, 17);
sheet.addMergedRegion(cellRangeAddress53);
// 第三行
// 报销部门
CellRangeAddress cellRangeAddress54 = new CellRangeAddress(20, 20, 0, 9);
sheet.addMergedRegion(cellRangeAddress54);
// 填写区域(年份)
CellRangeAddress cellRangeAddress55 = new CellRangeAddress(20, 20, 10, 11);
sheet.addMergedRegion(cellRangeAddress55);
// 填写区域(月份)
CellRangeAddress cellRangeAddress56 = new CellRangeAddress(20, 20, 13, 13);
sheet.addMergedRegion(cellRangeAddress56);
// 填写区域(日)
CellRangeAddress cellRangeAddress57 = new CellRangeAddress(20, 20, 14, 14);
sheet.addMergedRegion(cellRangeAddress57);
// 日填
CellRangeAddress cellRangeAddress58 = new CellRangeAddress(20, 20, 16, 17);
sheet.addMergedRegion(cellRangeAddress58);
// 单据及附件共
CellRangeAddress cellRangeAddress59 = new CellRangeAddress(20, 20, 20, 25);
sheet.addMergedRegion(cellRangeAddress59);
row = sheet.createRow(20);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("报销部门:${depart}"));
cell.setCellValue(new HSSFRichTextString(cell.getRichStringCellValue().toString().replace("${depart}", r2.getDepartment())));
// 这是填写的值
cell = row.createCell(10);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString(r2.getApplyTime().split("-")[0]));
cell = row.createCell(12);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("年"));
// 这是填写的值
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString(r2.getApplyTime().split("-")[1]));
cell = row.createCell(14);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("月"));
// 这是填写的值
cell = row.createCell(15);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString(r2.getApplyTime().split("-")[2]));
cell = row.createCell(16);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("日填"));
cell = row.createCell(20);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("附件共"));
// 填写的页码值
cell = row.createCell(26);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, true, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString(r2.getBillNum()));
cell = row.createCell(27);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("页"));
// 第四行
// 发生日期
CellRangeAddress cellRangeAddress60 = new CellRangeAddress(21, 21, 0, 1);
sheet.addMergedRegion(cellRangeAddress60);
// 用途
CellRangeAddress cellRangeAddress61 = new CellRangeAddress(21, 22, 2, 12);
sheet.addMergedRegion(cellRangeAddress61);
// 金额
CellRangeAddress cellRangeAddress62 = new CellRangeAddress(21, 22, 13, 17);
sheet.addMergedRegion(cellRangeAddress62);
// 备注
CellRangeAddress cellRangeAddress63 = new CellRangeAddress(21, 25, 18, 19);
sheet.addMergedRegion(cellRangeAddress63);
// 签字区域
CellRangeAddress cellRangeAddress64 = new CellRangeAddress(21, 25, 20, 27);
sheet.addMergedRegion(cellRangeAddress64);
row = sheet.createRow(21);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("发生日期"));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("用 途"));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("金额(元)"));
cell = row.createCell(18);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, true, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("备 注"));
// 这里填写备注的内容
cell = row.createCell(20);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, true));
cell.setCellValue(new HSSFRichTextString(r2.getRemarks()));
row = sheet.createRow(22);
row.setHeight((short) 300);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, false, true, true, false));
cell.setCellValue(new HSSFRichTextString("月"));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, false, true, true, false));
cell.setCellValue(new HSSFRichTextString("日"));
// 第六行
CellRangeAddress cellRangeAddress65 = new CellRangeAddress(23, 23, 2, 12);
sheet.addMergedRegion(cellRangeAddress65);
CellRangeAddress cellRangeAddress66 = new CellRangeAddress(23, 23, 13, 17);
sheet.addMergedRegion(cellRangeAddress66);
row = sheet.createRow(23);
row.setHeight((short) 600);
List<ReimbursementPurpose> listRP2 = r2.getListRP();
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 0 ? listRP2.get(0).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 0 ? listRP2.get(0).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 0 ? listRP2.get(0).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP2.size() > 0)
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP2.get(0).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP2.get(0).getPrice())));
p_t_1 = cell.getRichStringCellValue().toString();
// 第七行
CellRangeAddress cellRangeAddress67 = new CellRangeAddress(24, 24, 2, 12);
sheet.addMergedRegion(cellRangeAddress67);
CellRangeAddress cellRangeAddress68 = new CellRangeAddress(24, 24, 13, 17);
sheet.addMergedRegion(cellRangeAddress68);
row = sheet.createRow(24);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 1 ? listRP2.get(1).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 1 ? listRP2.get(1).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 1 ? listRP2.get(1).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP2.size() > 1)
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP2.get(1).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP2.get(1).getPrice())));
p_t_2 = cell.getRichStringCellValue().toString();
// 第八行
CellRangeAddress cellRangeAddress69 = new CellRangeAddress(25, 25, 2, 12);
sheet.addMergedRegion(cellRangeAddress69);
CellRangeAddress cellRangeAddress70 = new CellRangeAddress(25, 25, 13, 17);
sheet.addMergedRegion(cellRangeAddress70);
row = sheet.createRow(25);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 2 ? listRP2.get(2).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 2 ? listRP2.get(2).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 2 ? listRP2.get(2).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP2.size() > 2)
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP2.get(2).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP2.get(2).getPrice())));
p_t_3 = cell.getRichStringCellValue().toString();
// 第九行
CellRangeAddress cellRangeAddress71 = new CellRangeAddress(26, 26, 2, 12);
sheet.addMergedRegion(cellRangeAddress71);
CellRangeAddress cellRangeAddress72 = new CellRangeAddress(26, 26, 13, 17);
sheet.addMergedRegion(cellRangeAddress72);
// 部门审核
CellRangeAddress cellRangeAddress73 = new CellRangeAddress(26, 28, 18, 19);
sheet.addMergedRegion(cellRangeAddress73);
// 签字区域
CellRangeAddress cellRangeAddress74 = new CellRangeAddress(26, 28, 20, 22);
sheet.addMergedRegion(cellRangeAddress74);
// 领导审批
CellRangeAddress cellRangeAddress81 = new CellRangeAddress(26, 28, 23, 24);
sheet.addMergedRegion(cellRangeAddress81);
// 签字区域
CellRangeAddress cellRangeAddress82 = new CellRangeAddress(26, 28, 25, 27);
sheet.addMergedRegion(cellRangeAddress82);
row = sheet.createRow(26);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 3 ? listRP2.get(3).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 3 ? listRP2.get(3).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 3 ? listRP2.get(3).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP2.size() > 3)
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP2.get(3).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP2.get(3).getPrice())));
p_t_4 = cell.getRichStringCellValue().toString();
cell = row.createCell(18);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, true, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("部门审核"));
cell = row.createCell(23);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, true, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("领导审批"));
// 第十行
CellRangeAddress cellRangeAddress83 = new CellRangeAddress(27, 27, 2, 12);
sheet.addMergedRegion(cellRangeAddress83);
CellRangeAddress cellRangeAddress84 = new CellRangeAddress(27, 27, 13, 17);
sheet.addMergedRegion(cellRangeAddress84);
row = sheet.createRow(27);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 4 ? listRP2.get(4).getMonth() : ""));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 4 ? listRP2.get(4).getDay() : ""));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
cell.setCellValue(new HSSFRichTextString(listRP2.size() > 4 ? listRP2.get(4).getPurpose() : ""));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, true, false, false));
if (listRP2.size() > 4)
// cell.setCellValue(new HSSFRichTextString("¥:" + listRP2.get(4).getPrice()));
cell.setCellValue(new HSSFRichTextString(formatTosepara(listRP2.get(4).getPrice())));
p_t_5 = cell.getRichStringCellValue().toString();
// 第十一行
CellRangeAddress cellRangeAddress85 = new CellRangeAddress(28, 28, 0, 12);
sheet.addMergedRegion(cellRangeAddress85);
CellRangeAddress cellRangeAddress86 = new CellRangeAddress(28, 28, 13, 17);
sheet.addMergedRegion(cellRangeAddress86);
row = sheet.createRow(28);
row.setHeight((short) 600);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("合 计"));
// 计算合计
String p_t_all = "0.00";
p_t_all = priceUtils(p_t_1, p_t_2, "+");
p_t_all = priceUtils(p_t_all, p_t_3, "+");
p_t_all = priceUtils(p_t_all, p_t_4, "+");
p_t_all = priceUtils(p_t_all, p_t_5, "+");
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size12.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, false, false, false));
// cell.setCellValue(new HSSFRichTextString("¥:" + p_t_all));
cell.setCellValue(new HSSFRichTextString(formatTosepara(p_t_all.replace(",", ""))));
List<String> price_t = getCharacters(cell.getRichStringCellValue().toString());
// 第十二行
CellRangeAddress cellRangeAddress87 = new CellRangeAddress(29, 29, 0, 1);
sheet.addMergedRegion(cellRangeAddress87);
row = sheet.createRow(29);
row.setHeight((short) 600);
// 金额大写: ${7} 拾 ${6} 万 ${5} 仟 ${4} 佰 ${3} 拾 ${2} 元 ${1} 角 ${0} 分
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("金额大写:"));
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price_t.size() > 7 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price_t.size() > 7 ? price_t.get(price_t.size() - 8) : "⊗"));
cell = row.createCell(3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("拾"));
cell = row.createCell(4);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price_t.size() > 6 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price_t.size() > 6 ? price_t.get(price_t.size() - 7) : "⊗"));
cell = row.createCell(5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("万"));
cell = row.createCell(6);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price_t.size() > 5 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price_t.size() > 5 ? price_t.get(price_t.size() - 6) : "⊗"));
cell = row.createCell(7);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("仟"));
cell = row.createCell(8);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price_t.size() > 4 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price_t.size() > 4 ? price_t.get(price_t.size() - 5) : "⊗"));
cell = row.createCell(9);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("佰"));
cell = row.createCell(10);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price_t.size() > 3 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price_t.size() > 3 ? price_t.get(price_t.size() - 4) : "⊗"));
cell = row.createCell(11);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("拾"));
cell = row.createCell(12);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price_t.size() > 2 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price_t.size() > 2 ? price_t.get(price_t.size() - 3) : "⊗"));
cell = row.createCell(13);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("元"));
cell = row.createCell(14);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price_t.size() > 1 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price_t.size() > 1 ? price_t.get(price_t.size() - 2) : "⊗"));
cell = row.createCell(15);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString("角"));
cell = row.createCell(16);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb,
price_t.size() > 0 ? fontSize.name_size12.getValue() : fontSize.name_size18.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, true, true, true, false, false, false));
cell.setCellValue(new HSSFRichTextString(price_t.size() > 0 ? price_t.get(price_t.size() - 1) : "⊗"));
cell = row.createCell(17);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_RIGHT, false, false, false, false, true, true, true, false, true, false));
cell.setCellValue(new HSSFRichTextString("分"));
// 报销人签章
CellRangeAddress cellRangeAddress88 = new CellRangeAddress(29, 29, 18, 22);
sheet.addMergedRegion(cellRangeAddress88);
cell = row.createCell(18);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, true, true, true, false, true, false));
cell.setCellValue(new HSSFRichTextString("报销人签章:"));
// 签字区域
CellRangeAddress cellRangeAddress89 = new CellRangeAddress(29, 29, 23, 27);
sheet.addMergedRegion(cellRangeAddress89);
// 第十三行
// 会计主管
CellRangeAddress cellRangeAddress75 = new CellRangeAddress(30, 30, 0, 1);
sheet.addMergedRegion(cellRangeAddress75);
// 签字区域
CellRangeAddress cellRangeAddress76 = new CellRangeAddress(30, 30, 2, 8);
sheet.addMergedRegion(cellRangeAddress76);
// 会计
CellRangeAddress cellRangeAddress77 = new CellRangeAddress(30, 30, 9, 11);
sheet.addMergedRegion(cellRangeAddress77);
// 签字区域
CellRangeAddress cellRangeAddress78 = new CellRangeAddress(30, 30, 12, 17);
sheet.addMergedRegion(cellRangeAddress78);
// 出纳
CellRangeAddress cellRangeAddress79 = new CellRangeAddress(30, 30, 18, 20);
sheet.addMergedRegion(cellRangeAddress79);
// 签字区域
CellRangeAddress cellRangeAddress80 = new CellRangeAddress(30, 30, 21, 27);
sheet.addMergedRegion(cellRangeAddress80);
row = sheet.createRow(30);
row.setHeight((short) 600);
// 第一列
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("会计主管:"));
// 第二列
cell = row.createCell(9);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_CENTER, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("会计:"));
cell = row.createCell(18);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(setStyle(fontName.name_kt.getValue(), wb, fontSize.name_size14.getValue(),
HSSFCellStyle.ALIGN_LEFT, false, false, false, false, false, false, false, false, false, false));
cell.setCellValue(new HSSFRichTextString("出纳:"));
setRegionBorder(1, cellRangeAddress50, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress51, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress52, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress53, sheet, wb, true, true, false, false);
setRegionBorder(1, cellRangeAddress54, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress55, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress56, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress57, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress58, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress59, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress60, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress61, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress62, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress63, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress64, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress65, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress66, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress67, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress68, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress69, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress70, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress71, sheet, wb, true, true, true, false);
setRegionBorder(1, cellRangeAddress72, sheet, wb, true, true, false, false);
setRegionBorder(1, cellRangeAddress73, sheet, wb, true, true, true, true);
setRegionBorder(1, cellRangeAddress74, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress81, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress82, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress83, sheet, wb, true, true, false, true);
setRegionBorder(1, cellRangeAddress84, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress85, sheet, wb, false, true, true, true);
setRegionBorder(1, cellRangeAddress86, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress87, sheet, wb, false, true, true, false);
setRegionBorder(1, cellRangeAddress88, sheet, wb, false, true, false, false);
setRegionBorder(1, cellRangeAddress89, sheet, wb, false, true, false, true);
setRegionBorder(1, cellRangeAddress90, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress75, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress76, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress77, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress78, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress79, sheet, wb, false, false, false, false);
setRegionBorder(1, cellRangeAddress80, sheet, wb, false, false, false, false);
}
// 打印设置
PrintSetup ps = sheet.getPrintSetup();
// 印刷方向、true:横向、false:縦向
ps.setLandscape(true); // 横向
ps.setLandscape(false); // 縦向
// 设置用紙
ps.setPaperSize(PrintSetup.A4_PAPERSIZE);
sheet.setMargin(Sheet.LeftMargin, 1.5);// (左)
sheet.setMargin(Sheet.RightMargin, 0.5);// (右)
sheet.setMargin(Sheet.BottomMargin, 1.4);// (下)
sheet.setMargin(Sheet.TopMargin, 1.4);// (上)
// 启用“适合页面”打印选项的标志。(默认选择的是“将工作表调整为一页”)
sheet.setFitToPage(true);
// 设置sheet的Name
wb.setSheetName(0, "费用报销单");
String excelName = currTimeMillis + "费用报销单.xls";
// 文档输出
String path = "C:\\excel\\" + String.valueOf(LocalDate.now()).substring(0, 10) + "\\";
File dest = new File(path);
if (!dest.isDirectory() && !dest.exists())
dest.mkdirs();
FileOutputStream out = new FileOutputStream(path + excelName);
wb.write(out);
out.close();
excelUploadUrl = "http://xxx.xxxx.com:port/excel/" + String.valueOf(LocalDate.now()).substring(0, 10) + "/" + excelName;
return excelUploadUrl;
}
/**
* 单元格样式设置
*
* @param fortName 字体名称
* @param wb
* @param height 高度
* @param center HSSFCellStyle.ALIGN_CENTER
* @param flag true:竖排显示
* @param bold true:加粗
* @param single true:下划线
* @param dsingle true:双下划线
* @param border true:边框
* @param top true:上
* @param bottom 下
* @param left 左
* @param right 右
* @param warp true:自动换行
* @return
* @throws Exception
*/
private static HSSFCellStyle setStyle(String fortName, HSSFWorkbook wb, short height, short center, boolean flag,
boolean bold, boolean single, boolean dsingle, boolean border, boolean top, boolean bottom, boolean left,
boolean right, boolean warp) throws Exception {
// 设置单元格类型
HSSFFont font = wb.createFont();
font.setFontName(fortName);
font.setFontHeightInPoints(height);
font.setBoldweight((bold) ? HSSFFont.BOLDWEIGHT_BOLD : HSSFFont.BOLDWEIGHT_NORMAL);
if (single)
font.setUnderline(HSSFFont.U_SINGLE);
if (dsingle)
font.setUnderline(HSSFFont.U_DOUBLE);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(center);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 设置自动换行
if (warp)
cellStyle.setWrapText(true);
// 竖排显示
if (flag)
cellStyle.setRotation((short) 0xff);
if (border) {
if (top)
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 上边框
if (bottom)
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
if (left)
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框
if (right)
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框
}
return cellStyle;
}
/**
*
* @param border
* @param region
* @param sheet
* @param wb
* @param top 上
* @param bottom 下
* @param left 左
* @param right 右
*/
public static void setRegionBorder(int border, CellRangeAddress region, Sheet sheet, Workbook wb, boolean top,
boolean bottom, boolean left, boolean right) throws Exception {
if (top)
RegionUtil.setBorderTop(border, region, sheet, wb); // 上边框
if (bottom)
RegionUtil.setBorderBottom(border, region, sheet, wb); // 下边框
if (left)
RegionUtil.setBorderLeft(border, region, sheet, wb); // 左边框
if (right)
RegionUtil.setBorderRight(border, region, sheet, wb); // 右边框
}
/**
* 文本下划线设置
*
* @param detail
* @param font
* @return
*/
private static HSSFRichTextString UnderLineIndex(StringBuffer detail, HSSFFont font) throws Exception {
HSSFRichTextString richString = new HSSFRichTextString(detail.toString());
// 下划线的起始位置 结束位置
richString.applyFont(6, detail.length() - 1, font);
return richString;
}
public static HSSFFont getFont(HSSFWorkbook wb) throws Exception {
HSSFFont font = wb.createFont();
// 下划线
font.setUnderline(HSSFFont.U_SINGLE);
font.setFontName(fontName.name_kt.getValue());
font.setFontHeightInPoints(fontSize.name_size14.getValue());
return font;
}
/**
* @param args
*
*/
private static final String[] data = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
private static final String[] dataIndex = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
/**
* 将金额转换成汉字
*
* @param strPrice 0.25
* @return [零, 贰, 伍]
*/
private static List<String> getCharacters(String strPrice) throws Exception {
// 说明没有小数点
if (strPrice.indexOf(".") == -1)
strPrice += "-00";
else
strPrice = strPrice.replace(".", "-");
int bi = strPrice.indexOf("-");
int si = strPrice.length() - (bi + 1);
if (si == 1)
strPrice += "0";
bi = strPrice.indexOf("-");
si = strPrice.length() - (bi + 1);
String[] price_split = strPrice.split("-");
String[] array_before = intArray(price_split[0]);
String[] array_after = intArray(price_split[1]);
String[] capitalize0 = new String[array_before.length];
String[] capitalize1 = new String[array_after.length];
List<String> listAll = new ArrayList<>();
for (int i = 0; i < array_before.length; i++) {
for (int j = 0; j < dataIndex.length; j++) {
if (array_before[i].equals(dataIndex[j]))
listAll.add(data[Integer.parseInt(dataIndex[j])]);
}
}
for (int i = 0; i < array_after.length; i++) {
for (int j = 0; j < dataIndex.length; j++) {
if (array_after[i].equals(dataIndex[j]))
listAll.add(data[Integer.parseInt(dataIndex[j])]);
}
}
return listAll;
}
/**
* 将str转成数组
*
* @param num
* @return
*/
private static String[] intArray(String num) throws Exception {
// 新建一个数组用来保存num每一位的数字
String[] intArray = new String[num.length()];
for (int i = 0; i < num.length(); i++) {
// 遍历str将每一位数字添加如intArray
Character ch = num.charAt(i);
intArray[i] = String.valueOf(ch.toString());
}
return intArray;
}
/**
* 价格相加
*
* @return
*/
public static String priceUtils(String priceAll, String price, String util) throws Exception {
double newPrice = 0d;
priceAll = priceAll.replace("¥:", "");
price = price.replace("¥:", "");
if (StringUtils.isNullString(priceAll) && StringUtils.isNullString(price))
return "0.00";
if (StringUtils.isNullString(price))
return priceAll;
BigDecimal bd1 = new BigDecimal(priceAll.replace(",", ""));
BigDecimal bd2 = new BigDecimal(price.replace(",", ""));
if ("+".equals(util))
newPrice = bd1.add(bd2).doubleValue();
else
newPrice = bd1.subtract(bd2).doubleValue();
return String.valueOf(newPrice);
}
/**
* 价格加逗号,
*
* @param data 整型
* @return
*/
private static String formatTosepara(String data) throws Exception {
if (Double.parseDouble(data) >= 1)
return "¥:" + new DecimalFormat("#,###.00").format(Double.parseDouble(data));
return "¥:" + data;
}
public static void main(String[] args) {
List<Reimbursement> listR = new ArrayList<Reimbursement>();
Reimbursement r = new Reimbursement();
r.setUserName("test");
r.setDepartment("研发部");
r.setBillNum("0");
r.setApplyTime("2022-07-11");
r.setRemarks("备注");
listR.add(r);
/** 用途 */
List<ReimbursementPurpose> listRP = new ArrayList<>();
ReimbursementPurpose rp = new ReimbursementPurpose();
rp.setMonth("07");
rp.setDay("11");
rp.setPurpose("用户");
rp.setPrice("11.11");
listRP.add(rp);
listR.get(0).setListRP(listRP);
try {
System.out.println(reimbursementExcel(listR));
// createExcelWithImage();
String imageUrl = "http://excel.esmount.com:8099/excel/code/1657524231692.png";
System.out.println(imageUrl.substring(imageUrl.lastIndexOf("/")+1));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 字体
* @author think
*
*/
enum fontName {
name_kt(1, "楷体");
private Integer key;
private String value;
/**
* 枚举必须使用私有化
* @param key
* @param value
*/
private fontName(Integer key,String value) {
this.key = key;
this.value = value;
}
/** 获取key值 */
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
/** 获取对应的key的描述 */
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
/**
* 字体大小
* @author think
*
*/
enum fontSize {
name_size20(1, (short)20),
name_size18(1, (short)18),
name_size16(1, (short)16),
name_size14(1, (short)14),
name_size12(1, (short)12);
private Integer key;
private short value;
/**
* 枚举必须使用私有化
* @param key
* @param value
*/
private fontSize(Integer key,short value) {
this.key = key;
this.value = value;
}
/** 获取key值 */
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
/** 获取对应的key的描述 */
public short getValue() {
return value;
}
public void setValue(short value) {
this.value = value;
}
}
2:构造对象
// @Data:import lombok.Data;
@Data
public class Reimbursement {
private Integer reimbursementId = 0;
private String userName = "-";
private String department = "-";
private String billNum = "0";
private String applyTime = "-";
private String remarks = "-";
private String uploadUrl = "-";
/** 用途 */
private List<ReimbursementPurpose> listRP = new ArrayList<>();
}
@Data
public class ReimbursementPurpose {
private Integer reimbursementPurposeId = 0;
private Integer reimbursementId = 0;
private String month = "-";
private String day = "-";
private String purpose = "-";
private String price = "0.0";
}
3:条形码工具类
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* 条形码
*
* @author think
*
*/
public class GoogleBarCodeUtils {
/** 条形码宽度 */
private static final int WIDTH = 180;
/** 条形码高度 */
private static final int HEIGHT = 25;
/** 加文字 条形码 */
private static final int WORDHEIGHT = 40;
/**
* 设置 条形码参数
*/
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
private static final long serialVersionUID = 1L;
{
// 设置编码方式
put(EncodeHintType.CHARACTER_SET, "utf-8");
}
};
/**
* 生成 图片缓冲
*
* @author fxbin
* @param vaNumber VA 码
* @return 返回BufferedImage
*/
public static BufferedImage getBarCode(String vaNumber) {
try {
Code128Writer writer = new Code128Writer();
// 编码内容, 编码类型, 宽度, 高度, 设置参数
BitMatrix bitMatrix = writer.encode(vaNumber, BarcodeFormat.CODE_128, WIDTH, HEIGHT, hints);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 把带logo的二维码下面加上文字
*
* @author fxbin
* @param image 条形码图片
* @param words 文字
* @return 返回BufferedImage
*/
public static BufferedImage insertWords(BufferedImage image, String words) {
// 新的图片,把带logo的二维码下面加上文字
// if (!"".equals(words) && null != words) {
BufferedImage outImage = new BufferedImage(WIDTH, WORDHEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = outImage.createGraphics();
// 抗锯齿
setGraphics2D(g2d);
// 设置白色
setColorWhite(g2d);
// 画条形码到新的面板
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
// 画文字到新的面板
Color color = new Color(0, 0, 0);
g2d.setColor(color);
// 字体、字型、字号
g2d.setFont(new Font("微软雅黑", Font.PLAIN, 14));
// 文字长度
int strWidth = g2d.getFontMetrics().stringWidth(words);
// 总长度减去文字长度的一半 (居中显示)
int wordStartX = (WIDTH - strWidth) / 2;
// height + (outImage.getHeight() - height) / 2 + 12
int wordStartY = HEIGHT + 14;
// 画文字
g2d.drawString(words, wordStartX, wordStartY);
g2d.dispose();
outImage.flush();
return outImage;
// }
// return null;
}
/**
* 设置 Graphics2D 属性 (抗锯齿)
*
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
*/
private static void setGraphics2D(Graphics2D g2d) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
g2d.setStroke(s);
}
/**
* 设置背景为白色
*
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
*/
private static void setColorWhite(Graphics2D g2d) {
g2d.setColor(Color.WHITE);
// 填充整个屏幕
g2d.fillRect(0, 0, 600, 600);
// 设置笔刷
g2d.setColor(Color.BLACK);
}
/**
*
* @param image 用imageio读取的图片
* @param width 缩略图的目标宽度
* @param height 缩略图的目标高度
* @param save_file 保存图片的路径
* @return 无返回
*/
public static void thumbnail(BufferedImage image, int width, int height, String save_file) {
try {
Thumbnails.of(image).crop(Positions.CENTER)// 从图片中心截取缩略图
.size(width, height).toFile(save_file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取条形码路径
* @param context 非中文
* @return
* @throws Exception
*/
public static String getCode(String context, String currTimeMillis) throws Exception {
String path = "C://image//code//";
String suffix = ".png";
String name = currTimeMillis;
String pathAll = path + name + suffix;
File file = new File(pathAll);
BufferedImage image = insertWords(getBarCode(context), name);
ImageIO.write(image, "png", new File(pathAll));
// 线上环境链接
return "http://xxx.xxxxx.com:port/" + name + suffix;
}
}
<!-- 添加条形码 -->
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j-light</artifactId>
<version>2.0</version>
</dependency>
<!-- 压缩图片 -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>[0.4, 0.5)</version>
</dependency>