package com.utils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class ExcelUtil
{
/**
* 合并excel
*
* @param outputFileName
* @param inputFileNameArray
*/
public static void mergeExcel(String outputFileName, String... inputFileNameArray)
{
if (inputFileNameArray.length == 1)
{
System.out.println("至少需要两个文件才能合并,请验证!!!");
return;
}
try
{
XSSFWorkbook newExcelCreate = new XSSFWorkbook();
int a = 0;
for (String f : inputFileNameArray)
{
InputStream in = new FileInputStream(f);
XSSFWorkbook fromExcel = new XSSFWorkbook(in);
for (int i = 0; i < fromExcel.getNumberOfSheets(); i++)
{
//遍历每个sheet
a++;
XSSFSheet oldSheet = fromExcel.getSheetAt(i);
XSSFSheet newSheet = newExcelCreate.createSheet(a + "");
copySheet(newExcelCreate, oldSheet, newSheet);
}
}
FileOutputStream fileOut = new FileOutputStream(outputFileName);
newExcelCreate.write(fileOut);
fileOut.flush();
fileOut.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 合并excel的sheet
*
* @param outputFileName
* @param inputFileNameArray
*/
public static void mergeExcelForOneSheet(String outputFileName, String... inputFileNameArray)
{
if (inputFileNameArray.length == 1)
{
System.out.println("至少需要两个文件才能合并,请验证!!!");
return;
}
try
{
XSSFWorkbook newExcelCreate = new XSSFWorkbook();
XSSFSheet newSheet = newExcelCreate.createSheet("文字识别结果,仅供参考 ! 查询完成请务必与图片结果核对");
int rowNum = 0;
for (String f : inputFileNameArray)
{
InputStream in = new FileInputStream(f);
XSSFWorkbook fromExcel = new XSSFWorkbook(in);
for (int i = 0; i < fromExcel.getNumberOfSheets(); i++)
{
//遍历每个sheet
XSSFSheet oldSheet = fromExcel.getSheetAt(i);
for (int j = 0; j <= oldSheet.getLastRowNum(); j++)
{
rowNum++;
XSSFRow toRow = newSheet.createRow(rowNum);
copyRow(newExcelCreate, oldSheet.getRow(j), toRow);
}
}
}
FileOutputStream fileOut = new FileOutputStream(outputFileName);
newExcelCreate.write(fileOut);
fileOut.flush();
fileOut.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 读取excel
*
* @param filePath
* @return
*/
public static Workbook readExcel(String filePath)
{
if (null == filePath)
{
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
try
{
InputStream is = new FileInputStream(filePath);
if (".xls".equals(extString))
{
return new HSSFWorkbook(is);
}
else if (".xlsx".equals(extString))
{
return new XSSFWorkbook(is);
}
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
/**
* 读取Cell
*
* @param cell
* @return
*/
public static Object getCellFormatValue(Cell cell)
{
Object cellValue = null;
if (cell != null)
{
//判断cell类型
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
{
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA:
{
//判断cell是否为日期格式
if (DateUtil.isCellDateFormatted(cell))
{
//转换为日期格式YYYY-mm-dd
cellValue = cell.getDateCellValue();
}
else
{
//数字
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
}
case Cell.CELL_TYPE_STRING:
{
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
}
else
{
cellValue = "";
}
return cellValue;
}
/**
* 合并单元格
*
* @param fromSheet
* @param toSheet
*/
public static void mergeSheetAllRegion(XSSFSheet fromSheet, XSSFSheet toSheet)
{
int num = fromSheet.getNumMergedRegions();
CellRangeAddress cellR = null;
for (int i = 0; i < num; i++)
{
cellR = fromSheet.getMergedRegion(i);
toSheet.addMergedRegion(cellR);
}
}
/**
* 复制cell
*
* @param wb
* @param fromCell
* @param toCell
*/
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());
}
// 不同数据类型处理
int fromCellType = fromCell.getCellType();
toCell.setCellType(fromCellType);
if (fromCellType == XSSFCell.CELL_TYPE_NUMERIC)
{
if (DateUtil.isCellDateFormatted(fromCell))
{
toCell.setCellValue(fromCell.getDateCellValue());
}
else
{
toCell.setCellValue(fromCell.getNumericCellValue());
}
}
else if (fromCellType == XSSFCell.CELL_TYPE_STRING)
{
toCell.setCellValue(fromCell.getRichStringCellValue());
}
else if (fromCellType == XSSFCell.CELL_TYPE_BOOLEAN)
{
toCell.setCellValue(fromCell.getBooleanCellValue());
}
else if (fromCellType == XSSFCell.CELL_TYPE_ERROR)
{
toCell.setCellErrorValue(fromCell.getErrorCellValue());
}
else if (fromCellType == XSSFCell.CELL_TYPE_FORMULA)
{
toCell.setCellFormula(fromCell.getCellFormula());
}
else if (fromCellType == XSSFCell.CELL_TYPE_BLANK)
{
}
else
{
}
}
/**
* 复制行
*
* @param wb
* @param oldRow
* @param toRow
*/
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);
}
}
/**
* 复制Sheet
*
* @param wb
* @param fromSheet
* @param toSheet
*/
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);
}
}
/**
* 克隆cell style
*/
public static void copyCellStyle(XSSFCellStyle fromStyle, XSSFCellStyle toStyle)
{
//此一行代码搞定
toStyle.cloneStyleFrom(fromStyle);
}
/**
* 读取excel转成map
*
* @param filePath
* @param beginRow
* @return
*/
public static List<Map<String, String>> readExcelToMap(String filePath, int beginRow)
{
List<Map<String, String>> list = new ArrayList<>();
// 读取文件信息
Workbook wb = ExcelUtil.readExcel(filePath);
if (wb != null)
{
// 获取sheet总数
int sheetNum = wb.getNumberOfSheets();
for (int i = 0; i < sheetNum; i++)
{
// 获取sheet
Sheet sheet = wb.getSheetAt(i);
// 获取最大行数
int rowNum = sheet.getPhysicalNumberOfRows();
// 读取cell的标题
Row titleRow = sheet.getRow(beginRow - 1);
if (null == titleRow)
{
continue;
}
// 获取明细列数
int colNum = titleRow.getPhysicalNumberOfCells();
// 遍历明细
for (int x = beginRow; x < rowNum; x++)
{
// 行map
Map<String, String> map = new LinkedHashMap<>();
// 行信息
Row row = sheet.getRow(x);
if (row != null)
{
// 遍历行并获取到返回值
for (int j = 0; j < colNum; j++)
{
Cell titleCell = titleRow.getCell(j);
Cell cell = row.getCell(j);
String titleCellData = (String) ExcelUtil.getCellFormatValue(titleCell);
String cellData = (String) ExcelUtil.getCellFormatValue(cell);
map.put(titleCellData, cellData);
}
}
else
{
break;
}
list.add(map);
}
}
}
return list;
}
}
EXCEL读取、拷贝、合并的简单工具类
猜你喜欢
转载自blog.csdn.net/sinat_30637097/article/details/86244011
今日推荐
周排行