Commit 3816196a by wangweidong

土工平台修改

parent 462acf89
package com.patzn.cloud.service.lims.common;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.spire.xls.Worksheet;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import java.io.*;
import java.util.*;
public class POIUtil {
public static void copyCellStyle(XSSFCellStyle fromStyle, XSSFCellStyle toStyle) {
toStyle.cloneStyleFrom(fromStyle);
}
public static void mergeSheetAllRegion(XSSFSheet fromSheet, XSSFSheet toSheet) {
int num = fromSheet.getNumMergedRegions();
CellRangeAddress cellR ;
for (int i = 0; i < num; i++) {
cellR = fromSheet.getMergedRegion(i);
toSheet.addMergedRegion(cellR);
}
}
public static void copyCell(XSSFWorkbook wb, XSSFCell fromCell, XSSFCell toCell) {
XSSFCellStyle newstyle = wb.createCellStyle();
copyCellStyle(fromCell.getCellStyle(), newstyle);
//样式
toCell.setCellStyle(newstyle);
if (fromCell.getCellComment() != null) {
toCell.setCellComment(fromCell.getCellComment());
}
// 不同数据类型处理
CellType cellType = fromCell.getCellType();
toCell.setCellType(cellType);
if (fromCell == null || fromCell.equals(null) || fromCell.getCellType() == CellType.BLANK) {
toCell.setCellValue("");
} else {
//判断数据类型
switch (cellType) {
case FORMULA:
toCell.setCellValue(fromCell.getCellFormula());
break;
case NUMERIC:
toCell.setCellValue(fromCell.getNumericCellValue());
break;
case STRING:
toCell.setCellValue(fromCell.getStringCellValue());
break;
default:
break;
}
}
}
public static void copyRow(XSSFWorkbook wb, XSSFRow oldRow, XSSFRow toRow) {
toRow.setHeight(oldRow.getHeight());
for (Iterator cellIt = oldRow.cellIterator(); cellIt.hasNext(); ) {
XSSFCell tmpCell = (XSSFCell) cellIt.next();
XSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex());
copyCell(wb, tmpCell, newCell);
}
}
public static void copySheet(XSSFWorkbook wb, XSSFSheet fromSheet, XSSFSheet toSheet) {
mergeSheetAllRegion(fromSheet, toSheet);
//设置列宽
for (int i = 0; i <= fromSheet.getRow(fromSheet.getFirstRowNum()).getLastCellNum(); i++) {
toSheet.setColumnWidth(i, fromSheet.getColumnWidth(i));
}
for (Iterator rowIt = fromSheet.rowIterator(); rowIt.hasNext(); ) {
XSSFRow oldRow = (XSSFRow) rowIt.next();
XSSFRow newRow = toSheet.createRow(oldRow.getRowNum());
copyRow(wb, oldRow, newRow);
}
}
public static String hc(List<Workbook> workbooks, String uuid) {
ByteArrayOutputStream bos = null;
InputStream is = null;
FileOutputStream fileOut=null;
String filepath;
File file1 = new File("D://excel//封面.xlsx");
File file2 = new File("D://excel//检测报告(力学试验)三轴.xlsx");
// File file3 = new File("D://excel//检测报告(力学试验)手动十字板.xlsx");
// File file4 = new File("D://excel//检测报告(力学试验)休止角.xlsx");
// File file5 = new File("D://excel//检测报告(力学试验)袖珍贯入仪.xlsx");
// File file6 = new File("D://excel//检测报告(力学试验)直剪.xlsx");
try {
XSSFWorkbook workbook1 = new XSSFWorkbook(file1);
XSSFWorkbook workbook2 = new XSSFWorkbook(file2);
// XSSFWorkbook workbook3 = new XSSFWorkbook(file3);
// XSSFWorkbook workbook4 = new XSSFWorkbook(file4);
// XSSFWorkbook workbook5 = new XSSFWorkbook(file5);
// XSSFWorkbook workbook6 = new XSSFWorkbook(file6);
workbooks.add(workbook1);
workbooks.add(workbook2);
// workbooks.add(workbook3);
// workbooks.add(workbook4);
// workbooks.add(workbook5);
// workbooks.add(workbook6);
XSSFWorkbook newExcelCreat = new XSSFWorkbook();
for (Workbook workbook : workbooks) {
bos = new ByteArrayOutputStream();
workbook.write(bos);
byte[] barray = bos.toByteArray();
is = new ByteArrayInputStream(barray);
XSSFWorkbook fromExcel = new XSSFWorkbook(is);
int sheetNum = fromExcel.getNumberOfSheets();
for (int i = 0; i < sheetNum; i++) {
if (!fromExcel.isSheetHidden(i)){
XSSFSheet oldSheet = fromExcel.getSheetAt(i);
XSSFSheet newSheet = newExcelCreat.createSheet(oldSheet.getSheetName());
copyPicture(newExcelCreat,oldSheet,newSheet);
copySheet(newExcelCreat, oldSheet, newSheet);
}
}
}
int sheetNs = newExcelCreat.getNumberOfSheets();
// for (int i = 0; i < sheetNs; i++) {
// if (!newExcelCreat.isSheetHidden(i)){
// HSSFWorkbookUtil.insertImageByRowColFile(newExcelCreat,newExcelCreat.getSheetAt(i),0,0, new File("D://excel//图片1.png"));
// }
// }
String allFileName = "d:/excel/0.xlsx";
fileOut = new FileOutputStream(allFileName);
newExcelCreat.write(fileOut);
fileOut.flush();
/* bos = new ByteArrayOutputStream();
newExcelCreat.write(bos);
byte[] barray = bos.toByteArray();
is = new ByteArrayInputStream(barray);
filepath = hdfsPath + "twentyTwo/" + uuid + ".xls";
HdfsUtil.createFile(is, filepath);
log.info("文件路径为{}", filepath);*/
} catch (Exception i) {
return null;
} finally {
try {
fileOut.close();
is.close();
bos.close();
} catch (IOException i) {
i.printStackTrace();
}
}
return null;
}
public static Map<String, PictureData> getSheetPictrues07(int sheetNum,
XSSFSheet sheet, XSSFWorkbook workbook) {
Map<String, PictureData> sheetIndexPicMap = new HashMap<>();
for (POIXMLDocumentPart dr : sheet.getRelations()) {
if (dr instanceof XSSFDrawing) {
XSSFDrawing drawing = (XSSFDrawing) dr;
List<XSSFShape> shapes = drawing.getShapes();
for (XSSFShape shape : shapes) {
XSSFPicture pic = (XSSFPicture) shape;
XSSFClientAnchor anchor = pic.getPreferredSize();
CTMarker ctMarker = anchor.getFrom();
String picIndex = String.valueOf(sheetNum) + "_"
+ ctMarker.getRow() + "_" + ctMarker.getCol();
sheetIndexPicMap.put(picIndex, pic.getPictureData());
}
}
}
return sheetIndexPicMap;
}
public static void printImg(List<Map<String, PictureData>> sheetList) throws IOException {
for (Map<String, PictureData> map : sheetList) {
Object key[] = map.keySet().toArray();
for (int i = 0; i < map.size(); i++) {
// 获取图片流
PictureData pic = map.get(key[i]);
// 获取图片索引
String picName = key[i].toString();
// 获取图片格式
String ext = pic.suggestFileExtension();
byte[] data = pic.getData();
FileOutputStream out = new FileOutputStream("D:\\excel\\" + picName + "." + ext);
out.write(data);
out.close();
}
}
}
public static void main(String[] args) {
File file1 = new File("D://excel//封面.xlsx");
File file2 = new File("D://excel//检测报告(力学试验)三轴.xlsx");
}
/**
* 获取图片和位置 (xlsx)
*/
public static Map<String, Object> getPicturesFromXSSFSheet (XSSFSheet sheet) throws IOException {
Map<String, Object> map = new HashMap<String, Object>();
List<POIXMLDocumentPart> list = sheet.getRelations();
for (POIXMLDocumentPart part : list) {
if (part instanceof XSSFDrawing) {
XSSFDrawing drawing = (XSSFDrawing) part;
List<XSSFShape> shapes = drawing.getShapes();
for (XSSFShape shape : shapes) {
XSSFPicture picture = (XSSFPicture) shape;
XSSFClientAnchor anchor = picture.getPreferredSize();
map.put("pictureAnchor", anchor);
map.put("pictureByteArray", picture.getPictureData().getData());
map.put("pictureType", picture.getPictureData().getPictureType());
}
}
}
return map;
}
public static void copyPicture(XSSFWorkbook workbook,XSSFSheet sourceSheet,XSSFSheet targetSheet) throws IOException {
XSSFDrawing drawing=targetSheet.createDrawingPatriarch();
Map<String, Object> sourceSheetPicture=getPicturesFromXSSFSheet(sourceSheet);
drawing.createPicture((XSSFClientAnchor) sourceSheetPicture.get("pictureAnchor"),
workbook.addPicture((byte[])sourceSheetPicture.get("pictureByteArray"),
Integer.parseInt(sourceSheetPicture.get("pictureType").toString())));
}
}
\ No newline at end of file
...@@ -12,7 +12,6 @@ import com.patzn.cloud.oss.starter.OssClient; ...@@ -12,7 +12,6 @@ import com.patzn.cloud.oss.starter.OssClient;
import com.patzn.cloud.oss.starter.OssFileResult; import com.patzn.cloud.oss.starter.OssFileResult;
import com.patzn.cloud.service.lims.common.AsposeUtil; import com.patzn.cloud.service.lims.common.AsposeUtil;
import com.patzn.cloud.service.lims.common.HSSFWorkbookUtil; import com.patzn.cloud.service.lims.common.HSSFWorkbookUtil;
import com.patzn.cloud.service.lims.common.POIUtil;
import com.patzn.cloud.service.lims.common.StringHandleUtils; import com.patzn.cloud.service.lims.common.StringHandleUtils;
import com.patzn.cloud.service.lims.soil.mapper.SoilExpReportMapper; import com.patzn.cloud.service.lims.soil.mapper.SoilExpReportMapper;
import com.patzn.cloud.service.lims.soil.service.*; import com.patzn.cloud.service.lims.soil.service.*;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment