Commit a253aa31 by wangweidong

土工平台修改

parent 31c51e90
package com.patzn.cloud.service.lims.hmhj.common;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
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.*;
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 java.io.*;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
public class HSSFWorkbookUtil {
public static HSSFWorkbook replaceModel(Map item, HSSFWorkbook wb) {
try {
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while(rows.hasNext()){
HSSFRow row = (HSSFRow) rows.next();
if(row!=null) {
int num = row.getLastCellNum();
for(int i=0;i<num;i++) {
HSSFCell cell= row.getCell(i);
if(cell!=null) {
cell.setCellType(CellType.STRING);
}
if(cell==null || cell.getStringCellValue()==null) {
continue;
}
String value= cell.getStringCellValue();
if(!"".equals(value)) {
Set<String> keySet = item.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String text = it.next();
if(value.equalsIgnoreCase(text)) {
cell.setCellValue((String)item.get(text));
break;
}
}
} else {
cell.setCellValue("");
}
}
}
}
// 输出文件
} catch (Exception e) {
e.printStackTrace();
}
return wb;
}
public static XSSFWorkbook replaceModel(Map item, XSSFWorkbook wb) {
try {
XSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while(rows.hasNext()){
XSSFRow row = (XSSFRow) rows.next();
if(row!=null) {
int num = row.getLastCellNum();
for(int i=0;i<num;i++) {
XSSFCell cell= row.getCell(i);
if(cell!=null) {
cell.setCellType(CellType.STRING);
}
if(cell==null || cell.getStringCellValue()==null) {
continue;
}
String value= cell.getStringCellValue();
if(!"".equals(value)) {
Set<String> keySet = item.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String text = it.next();
if(value.equalsIgnoreCase(text)) {
cell.setCellValue((String)item.get(text));
break;
}
}
} else {
cell.setCellValue("");
}
}
}
}
// 输出文件
} catch (Exception e) {
e.printStackTrace();
}
return wb;
}
public static String getRawValue(XSSFCell cell) {
if (null == cell) {
return "";
}
return cell.getRawValue();
}
public static Object getJavaValue(XSSFCell cell) {
if (null==cell){
return "";
}
Object o = null;
CellType cellType = cell.getCellType();
if (cellType.equals(CellType.BLANK)){
o = "";
}else if (cellType.equals(CellType.BOOLEAN)){
o = cell.getBooleanCellValue();
}else if (cellType.equals(CellType.ERROR)){
o = "";
}else if (cellType.equals(CellType.NUMERIC)){
o = getValueOfNumericCell(cell);
}else if (cellType.equals(CellType.FORMULA)){
try {
o = getValueOfNumericCell(cell);
} catch (IllegalStateException e) {
try {
o = cell.getRichStringCellValue().toString();
} catch (IllegalStateException e2) {
o = cell.getErrorCellString();
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
o = cell.getRichStringCellValue().getString();
}
return o;
}
public static Object getJavaCellValue(Cell cell) {
Object o = null;
CellType cellType = cell.getCellTypeEnum();
if (cellType.equals(CellType.BLANK)){
o = "";
}else if (cellType.equals(CellType.BOOLEAN)){
o = cell.getBooleanCellValue();
}else if (cellType.equals(CellType.ERROR)){
o = "";
}else if (cellType.equals(CellType.NUMERIC)){
o = cell.getNumericCellValue();
}else if (cellType.equals(CellType.FORMULA)){
try {
o = cell.getStringCellValue();
} catch (IllegalStateException e) {
try {
o = cell.getRichStringCellValue().toString();
} catch (IllegalStateException e2) {
o ="";
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
o = cell.getRichStringCellValue().getString();
}
return o;
}
public static Object getCellValue(Cell cell) {
Object o = null;
CellType cellType = cell.getCellType();
if (cellType.equals(CellType.BLANK)){
o = "";
}else if (cellType.equals(CellType.BOOLEAN)){
o = cell.getBooleanCellValue();
}else if (cellType.equals(CellType.ERROR)){
o = "";
}else if (cellType.equals(CellType.NUMERIC)){
DecimalFormat df = new DecimalFormat("0");
o = df.format(cell.getNumericCellValue());
}else if (cellType.equals(CellType.FORMULA)){
try {
o = cell.getStringCellValue();
} catch (IllegalStateException e) {
try {
o = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e2) {
o ="";
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
o = cell.getRichStringCellValue().getString();
}
return o;
}
// 获取数字类型的cell值
private static Object getValueOfNumericCell(XSSFCell cell) {
Boolean isDate = DateUtil.isCellDateFormatted(cell);
Double d = cell.getNumericCellValue();
Object o = null;
if (isDate) {
o = DateFormat.getDateTimeInstance()
.format(cell.getDateCellValue());
} else {
o = getRealStringValueOfDouble(d);
}
return o;
}
// 处理科学计数法与普通计数法的字符串显示,尽最大努力保持精度
private static String getRealStringValueOfDouble(Double d) {
String doubleStr = d.toString();
boolean b = doubleStr.contains("E");
int indexOfPoint = doubleStr.indexOf('.');
if (b) {
int indexOfE = doubleStr.indexOf('E');
// 小数部分
BigInteger xs = new BigInteger(doubleStr.substring(indexOfPoint
+ BigInteger.ONE.intValue(), indexOfE));
// 指数
int pow = Integer.valueOf(doubleStr.substring(indexOfE
+ BigInteger.ONE.intValue()));
int xsLen;
if(xs.compareTo(new BigInteger("0"))==0){
//小数部分为0要单独处理,不然最后会多出一个0
xsLen = 0;
}else{
xsLen = xs.toByteArray().length;
}
int scale = xsLen - pow > 0 ? xsLen - pow : 0;
doubleStr = String.format("%." + scale + "f", d);
} else {
Pattern p = Pattern.compile(".0$");
java.util.regex.Matcher m = p.matcher(doubleStr);
if (m.find()) {
doubleStr = doubleStr.replace(".0", "");
}
}
return doubleStr;
}
/**
* 图片插入到excel固定的cell
* @throws IOException
* @throws FileNotFoundException
*/
public static XSSFWorkbook insertImage( XSSFWorkbook wb,Map<String,File> imageFileMap,boolean resize) throws FileNotFoundException, IOException {
XSSFSheet sheet = wb.getSheetAt(0);
//数据行数
int n = sheet.getLastRowNum();
Map<String, Integer> nameIndex = addPicture2Workbook(wb,imageFileMap);
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
for (int i = 0; i < n; i++) {
XSSFRow xssfRow=sheet.getRow(i);
if (null!=xssfRow){
int column0=xssfRow.getFirstCellNum();
int column1=xssfRow.getLastCellNum();
for (int j=column0 ;j<column1;j++) {
XSSFCell cell= xssfRow.getCell(j);
if (null!=cell){
String qrcodeName=cell.getStringCellValue();
if (nameIndex.containsKey(qrcodeName)) {
anchor.setCol1(j);
anchor.setRow1(i);
System.out.println("setCol1:"+j);
System.out.println("setRow1:"+i);
Picture pict = drawing.createPicture(anchor, nameIndex.get(qrcodeName));
// 计算单元格的长宽
if (resize){
// 指定我想要的长宽
double standardWidth = 600;
double standardHeight = 830;
double w = sheet.getColumnWidth(cell.getColumnIndex());
double h = cell.getRow().getHeight();
double cellWidth = sheet.getColumnWidthInPixels(cell.getColumnIndex());
double cellHeight = cell.getRow().getHeightInPoints()/72*96;
// 计算需要的长宽比例的系数
double a = standardWidth / cellWidth;
double b = standardHeight / cellHeight;
pict.resize(a,b);
}else{
pict.resize();
}
}
}
}
}
}
return wb;
}
public static XSSFWorkbook insertImageByIO( XSSFWorkbook wb,Map<String,InputStream> imageFileMap,boolean resize) throws FileNotFoundException, IOException {
XSSFSheet sheet = wb.getSheetAt(0);
//数据行数
int n = sheet.getLastRowNum();
Map<String, Integer> nameIndex = addPicture2WorkbookByIO(wb,imageFileMap);
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
for (int i = 0; i < n; i++) {
XSSFRow xssfRow=sheet.getRow(i);
if (null!=xssfRow){
int column0=xssfRow.getFirstCellNum();
int column1=xssfRow.getLastCellNum();
for (int j=column0 ;j<column1;j++) {
XSSFCell cell= xssfRow.getCell(j);
if (null!=cell){
String qrcodeName=HSSFWorkbookUtil.getJavaValue(cell).toString();
System.out.println(qrcodeName);
if (nameIndex.containsKey(qrcodeName)) {
anchor.setCol1(j);
anchor.setRow1(i);
System.out.println("setCol1:"+j);
System.out.println("setRow1:"+i);
Picture pict = drawing.createPicture(anchor, nameIndex.get(qrcodeName));
// 计算单元格的长宽
cell.setCellValue("");
if (resize){
// 指定我想要的长宽
double standardWidth = 600;
double standardHeight = 830;
double w = sheet.getColumnWidth(cell.getColumnIndex());
double h = cell.getRow().getHeight();
double cellWidth = sheet.getColumnWidthInPixels(cell.getColumnIndex());
double cellHeight = cell.getRow().getHeightInPoints()/72*96;
// 计算需要的长宽比例的系数
double a = standardWidth / cellWidth;
double b = standardHeight / cellHeight;
pict.resize(a,b);
}else{
pict.resize();
}
}
}
}
}
}
return wb;
}
public static XSSFWorkbook insertImageByIO( XSSFWorkbook wb,Map<String,InputStream> imageFileMap,boolean resize,int index) throws FileNotFoundException, IOException {
XSSFSheet sheet = wb.getSheetAt(index);
//数据行数
int n = sheet.getLastRowNum();
Map<String, Integer> nameIndex = addPicture2WorkbookByIO(wb,imageFileMap);
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
for (int i = 0; i < n; i++) {
XSSFRow xssfRow=sheet.getRow(i);
if (null!=xssfRow){
int column0=xssfRow.getFirstCellNum();
int column1=xssfRow.getLastCellNum();
for (int j=column0 ;j<column1;j++) {
XSSFCell cell= xssfRow.getCell(j);
if (null!=cell){
String qrcodeName=HSSFWorkbookUtil.getJavaValue(cell).toString();
System.out.println(qrcodeName);
if (nameIndex.containsKey(qrcodeName)) {
anchor.setCol1(j);
anchor.setRow1(i);
System.out.println("setCol1:"+j);
System.out.println("setRow1:"+i);
Picture pict = drawing.createPicture(anchor, nameIndex.get(qrcodeName));
// 计算单元格的长宽
cell.setCellValue("");
if (resize){
// 指定我想要的长宽
double standardWidth = 600;
double standardHeight = 830;
double w = sheet.getColumnWidth(cell.getColumnIndex());
double h = cell.getRow().getHeight();
double cellWidth = sheet.getColumnWidthInPixels(cell.getColumnIndex());
double cellHeight = cell.getRow().getHeightInPoints()/72*96;
// 计算需要的长宽比例的系数
double a = standardWidth / cellWidth;
double b = standardHeight / cellHeight;
pict.resize(a,b);
}else{
pict.resize();
}
}
}
}
}
}
return wb;
}
private static Map<String, Integer> addPicture2Workbook(XSSFWorkbook wb, Map<String, File> qr) throws FileNotFoundException, IOException {
Map<String, Integer> indx = Maps.newHashMap();
for (Map.Entry<String, File> kv : qr.entrySet()) {
indx.put(kv.getKey(), wb.addPicture(new FileInputStream(kv.getValue()), XSSFWorkbook.PICTURE_TYPE_PNG));
}
System.out.println("pictures : " + indx.size());
return indx;
}
private static Map<String, Integer> addPicture2WorkbookByIO(XSSFWorkbook wb, Map<String, InputStream> qr) throws FileNotFoundException, IOException {
Map<String, Integer> indx = Maps.newHashMap();
for (Map.Entry<String, InputStream> kv : qr.entrySet()) {
indx.put(kv.getKey(), wb.addPicture(kv.getValue(), XSSFWorkbook.PICTURE_TYPE_PNG));
}
System.out.println("pictures : " + indx.size());
return indx;
}
public static void insertImageByRowColFile(XSSFWorkbook wb ,XSSFSheet sheet, int row, int col, File file) {
XSSFRow xssfRow=sheet.getRow(row);
if (null!=xssfRow){
XSSFCell cell= xssfRow.getCell(col);
if (null!=cell){
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(row);
anchor.setRow1(col);
try {
Picture pict = drawing.createPicture(anchor, wb.addPicture(new FileInputStream(file), XSSFWorkbook.PICTURE_TYPE_PNG));
// 计算单元格的长宽
pict.resize();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
public static XSSFWorkbook replaceModel(Map item, XSSFWorkbook wb,int index) {
try {
XSSFSheet sheet = wb.getSheetAt(index);
Iterator rows = sheet.rowIterator();
while(rows.hasNext()){
XSSFRow row = (XSSFRow) rows.next();
if(row!=null) {
int num = row.getLastCellNum();
for(int i=0;i<num;i++) {
XSSFCell cell= row.getCell(i);
if(cell==null || StringUtils.isBlank(getJavaValue(cell).toString())) {
continue;
}
String value= getJavaValue(cell).toString();
if(!"".equals(value)) {
Set<String> keySet = item.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String text = it.next();
if(value.contains(text)) {
String txt = value.replace(text,(String)item.get(text));
cell.setCellValue(txt);
break;
}
}
} else {
cell.setCellValue("");
}
}
}
}
// 输出文件
} catch (Exception e) {
e.printStackTrace();
}
return wb;
}
public static XSSFWorkbook replaceModel(Map item, XSSFWorkbook wb,XSSFSheet sheet) {
try {
Iterator rows = sheet.rowIterator();
while(rows.hasNext()){
XSSFRow row = (XSSFRow) rows.next();
if(row!=null) {
int num = row.getLastCellNum();
for(int i=0;i<num;i++) {
XSSFCell cell= row.getCell(i);
if(cell==null || StringUtils.isBlank(getJavaValue(cell).toString())) {
continue;
}
String value= getJavaValue(cell).toString();
if(!"".equals(value)) {
Set<String> keySet = item.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String text = it.next();
if(value.contains(text)) {
String txt = value.replace(text,(String)item.get(text));
cell.setCellValue(txt);
break;
}
}
} else {
cell.setCellValue("");
}
}
}
}
// 输出文件
} catch (Exception e) {
e.printStackTrace();
}
return wb;
}
public static XSSFWorkbook getWorkbookByIO(InputStream io) {
try {
XSSFWorkbook xssfWorkbook= new XSSFWorkbook(io);
io.close();
return xssfWorkbook;
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
try {
io.close();
}catch (Exception e){
}
}
}
}
......@@ -10,16 +10,14 @@ import com.patzn.cloud.service.hmhj.entity.*;
import com.patzn.cloud.service.hmhj.enums.EntrustSampleItemStatusEnum;
import com.patzn.cloud.service.hmhj.enums.EntrustSampleStatusEnum;
import com.patzn.cloud.service.hmhj.vo.EntrustSampleItemVO;
import com.patzn.cloud.service.lims.common.HSSFWorkbookUtil;
import com.patzn.cloud.service.lims.common.StringHandleUtils;
import com.patzn.cloud.service.lims.hmhj.common.HSSFWorkbookUtil;
import com.patzn.cloud.service.lims.hmhj.mapper.EntrustSampleItemMapper;
import com.patzn.cloud.service.lims.hmhj.service.*;
import com.patzn.cloud.commons.service.impl.BaseServiceImpl;
import com.patzn.poibox.xwpf.HSSFWorkbookUtil;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -32,7 +30,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
......
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