excel转为图片
这个操作是要根据excel一行一行画出来的
package com.gxuwz.zjh.util;import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import com.gxuwz.zjh.entity.Grid;
import com.gxuwz.zjh.entity.UserCell;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;/*** @author FangZenglin* @date 2023年10月13日16:38*/
@Component
public class ExcelZhuanTuPian {public void toImage(String dizhipath,String mubiaopath){test(dizhipath,mubiaopath);}public static void test(String path,String mubiaopath) {int sheetNum = 0;int imageWidth = 0;int imageHeight = 0;InputStream inputStream;try {inputStream = new FileInputStream(path);Workbook workbook;Sheet sheet;List<List<String>> excelList;int bgColorFlag = 1;if (path.split("\\.")[1].equals("xlsx")) {workbook = new XSSFWorkbook(inputStream);excelList = readXlsx(path, sheetNum);} else {workbook = new HSSFWorkbook(inputStream);excelList = readXls(path, sheetNum);bgColorFlag = 0;}sheet = workbook.getSheetAt(sheetNum);List<CellRangeAddress> rangeAddress = sheet.getMergedRegions();int totalRow = excelList.size() + 1;int totalCol = excelList.get(0).size();UserCell[][] cells = new UserCell[totalRow + 1][totalCol + 1];int[] rowPixPos = new int[totalRow + 1];rowPixPos[0] = 0;int[] colPixPos = new int[totalCol + 1];colPixPos[0] = 0;for (int i = 0; i < totalRow - 1; i++) {for (int j = 0; j < totalCol; j++) {cells[i][j] = new UserCell();cells[i][j].setCell(sheet.getRow(i).getCell(j));cells[i][j].setRow(i);cells[i][j].setCol(j);boolean ifShow = !(sheet.isColumnHidden(j) || sheet.getRow(i).getZeroHeight());cells[i][j].setShow(ifShow);float widthPix = !ifShow ? 0: (sheet.getColumnWidthInPixels(j)