Excel中的模版是这样的 一个PNG 或者jpg 另外有一段表格 如图
首先呢,我们会想到poi这个工具
然后我的思路是先把图片取出来,然后利用G2P 将表格画出来,这样一想,就很简单了。下面看代码
取表格
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFFont;
import sun.awt.SunHints;
public class DrawFromExcel {
public static void main(String[] args) {
}
public static void ImgUtile(String file,int from,int from1,int to,int to1) throws Exception {
// 给定两个初始值,标志出导出区域,两个行列组合的单元格
int[] fromIndex = {from,from1};
int[] toIndex = {to,to1};
int imageWidth = 0;//图片初始长度值
int imageHeight =0;//初始宽带值
Workbook wb = WorkbookFactory.create(new FileInputStream(file));//打开路径 (名字可动态传值方便)
Sheet sheet = wb.getSheetAt(0);
List<CellRangeAddress> rangeAddress = sheet.getMergedRegions(); // 获取整个sheet中合并单元格组合的集合
// 首先做初步的边界检测,如果指定区域是不合法的则抛出异常
int rowSum = sheet.getPhysicalNumberOfRows();
int colSum = sheet.getRow(0).getPhysicalNumberOfCells();
if (fromIndex[0] > rowSum || fromIndex[0] > toIndex[0] || toIndex[0] > rowSum) {
throw new Exception("the rowIndex of the area is wrong!");
}
if (fromIndex[1] > colSum || fromIndex[1] > toIndex[1] || toIndex[1] > colSum) {
throw new Exception("the colIndex of the area is wrong!");
}
// 计算实际需要载入内存的二维Cell数组的大小,剔除隐藏行列
int rowSize = toIndex[0]-fromIndex[0];//区域划分行
System.out.println("rowSize:"+rowSize);
int colSize = toIndex[1]-fromIndex[1];//区域划分列
System.out.println("colSize:"+colSize);
// 遍历需要扫描的区域
UserCell[][] cells = new UserCell[rowSize][colSize];
int[] rowPixPos = new int[rowSize + 1];
rowPixPos[0] = 0;
int[] colPixPos = new int[colSize + 1];
colPixPos[0] = 0;
for (int i = fromIndex[0]; i < toIndex[0]; i++) {
for (int j = fromIndex[1]; j < toIndex[1]; j++) {
cells[i-fromIndex[0]][j-fromIndex[1]] = new UserCell();
if (sheet.getRow(i).getCell(j) !=null) {
cells[i-fromIndex[0]][j-fromIndex[1]].setCell(sheet.getRow(i).getCell(j));
}
cells[i-fromIndex[0]][j-fromIndex[1]].setRow(i);
cells[i-fromIndex[0]][j-fromIndex[1]].setCol(j);
boolean ifShow=(i>=fromIndex[0]) && (j>=fromIndex[1]); //首先行列要在指定区域之间
ifShow=ifShow && !(sheet.isColumnHidden(j) || sheet.getRow(i).getZeroHeight()); //其次行列不可以隐藏
cells[i-fromIndex[0]][j-fromIndex[1]].setShow(ifShow);
// 计算所求区域宽度
float widthPix = !ifShow ? 0 : sheet.getColumnWidthInPixels(j); // 如果该单元格是隐藏的,则置宽度为0
if (i == fromIndex[0]) {
imageWidth += widthPix;
}
colPixPos[j-fromIndex[1]+1] = (int) (widthPix * 1.15 + colPixPos[j-fromIndex[1]]);
}
// 计算所求区域高度
boolean ifShow=(i>=fromIndex[0]); //行序列在指定区域中间
ifShow=ifShow && !sheet.getRow(i).getZeroHeight(); //行序列不能隐藏
float heightPoint = !ifShow ? 0 : sheet.getRow(i).getHeightInPoints(); // 如果该单元格是隐藏的,则置高度为0
imageHeight += heightPoint;
rowPixPos[i-fromIndex[0]+1] = (int) (heightPoint * 96 / 72) + rowPixPos[i-fromIndex[0]];
}
imageHeight = imageHeight * 96 / 72;
imageWidth = imageWidth * 115 / 100;
wb.close();
List<Grid> grids = new ArrayList<Grid>();
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
Grid grid = new Grid();
// 设置坐标和宽高
grid.setX(colPixPos[j]);
grid.setY(rowPixPos[i]);
grid.setWidth(colPixPos[j + 1] - colPixPos[j]);
grid.setHeight(rowPixPos[i + 1] - rowPixPos[i]);
grid.setRow(cells[i][j].getRow());
grid.setCol(cells[i][j].getCol());
grid.setShow(cells[i][j].isShow());
// 判断是否为合并单元格
int[] isInMergedStatus