import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.utilities.PdfTable;
import com.spire.pdf.utilities.PdfTableExtractor;
//import com.spire.xls.ExcelVersion;
//import com.spire.xls.*;
import org.apache.commons.io.FileUtils;
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.*;
/**
* 1.读取pdf里面的excel表格
* 2.写入excel中
* 3.读取excel数据
*/
public class ExcelCompare {
public static void main(String[] args) {
String filePath = "E:\\tmp\\test1.pdf";
String textPath = "E:\\tmp\\test1.txt";
String excelPath = "E:\\tmp\\test1.xlsx";
String excelPath2 = "E:\\tmp\\test2.xlsx";
// // 加载pdf文件
// PdfDocument pdf = new PdfDocument();
// pdf.loadFromFile(filePath);
// //保存为Excel文档
// pdf.saveToFile(excelPath, FileFormat.XLSX);
// pdf.dispose();
//加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile(filePath);
//创建StringBuilder类的实例
StringBuilder builder = new StringBuilder();
//抽取表格
PdfTableExtractor extractor = new PdfTableExtractor(pdf);
PdfTable[] tableLists ;
// PDF总页数
int count = pdf.getPages().getCount();
if(count > 0) {
// 创建一个工作簿对象
// Workbook workbook = new Workbook();
XSSFWorkbook workbook = new XSSFWorkbook();
// 删除默认工作表
// workbook..getWorksheets().clear();
for (int page = 0; page < count; page++)
{
tableLists = extractor.extractTable(page);
if (tableLists != null && tableLists.length > 0)
{
for (PdfTable table : tableLists)
{
// 创建EXCEL的sheet页
String sheetName = String.format("sheetName - %d", page);
// Worksheet sheet = workbook.getWorksheets().add(sheetName);
XSSFSheet sheet = workbook.createSheet();
int row = table.getRowCount();
int column = table.getColumnCount();
for (int i = 0; i < row; i++)
{
// 创建行
XSSFRow head = sheet.createRow(i);
XSSFCell cell = null;
for (int j = 0; j < column; j++)
{
String text = table.getText(i, j);
builder.append(text+" ");
//将数据插入特定单元格
// sheet.get(i + 1, j + 1).setText(text);
cell = head.createCell(j);
cell.setCellValue(text);
}
builder.append("\r\n");
}
//自动调整列宽
// for (int sheetColNum = 0; sheetColNum < sheet.getColumns().length; sheetColNum++) {
// sheet.autoFitColumn(sheetColNum + 1);
// }
for (int sheetColNum = 0; sheetColNum < sheet.getRow(1).getLastCellNum(); sheetColNum++) {
sheet.autoSizeColumn((short)sheetColNum, true);
}
}
}
}
// 保存前判断是否已经存在EXCEL文件,存在则删除
File fileOne = new File(excelPath2);
if (fileOne.exists() && fileOne.isFile()) {
fileOne.delete();
}
// 将工作簿保存为 Excel 文件
// workbook.saveToFile(excelPath2, ExcelVersion.Version2016);
try {
fileOne.createNewFile();
//文件输出流
FileOutputStream stream = FileUtils.openOutputStream(fileOne);
//写入
workbook.write(stream);
//关闭输出流
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("将" + filePath + "转换为EXCEL结束!!!!!");
}
//将提取的表格内容写入txt文档
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(textPath);
fileWriter.write(builder.toString());
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
// 读取excel
FileInputStream xlsStream = null;
try {
// Excel工作簿 输入流
xlsStream = new FileInputStream(new File("E:\\tmp\\test_tmp.xlsx"));
// 构造工作簿对象
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(xlsStream);
// 获取工作表,这里获取的是第一个sheet,
// 如果一个工作薄对象有多个sheet的话,就需要遍历获取多个
XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0);
// 获取行,行号作为参数传递给getRow方法
XSSFRow row = sheetAt.getRow(0);
// 获取单元格,row已经确定了行号,列号作为参数传递给getCell,就可以获得相应的单元格了
XSSFCell readCell = row.getCell(0);
// 获取单元格的值
String cellValue = readCell.getStringCellValue();
System.out.println("获取到的数据是:" + cellValue);
//遍历excel
for (int i = 0; i <= sheetAt.getLastRowNum(); i++) {
row = sheetAt.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
readCell = row.getCell(j);
System.out.println("第" + (i+1) + "行,第"+ j + "列,值为:" + readCell);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (xlsStream != null) {
try {
xlsStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}