POI
dependencies
<dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
</dependency>
Create Excel
@Test
public void createExcel() throws IOException {
// Create file
FileOutputStream fos = new FileOutputStream(new File("./poi-excel.xlsx"));
// Create blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
// Create a blank spreadsheet
XSSFSheet spreadsheet = workbook.createSheet("MySheet");
// Set column width
spreadsheet.setColumnWidth(0, 800);
// Merge
spreadsheet.addMergedRegion(new CellRangeAddress(
1, // first row
1, // last row
1, // first column
4 // last column
));
XSSFRow row = spreadsheet.createRow(0);
// Set row height
row.setHeight((short) 800);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Biu biu biu ~ ~ ~ ");
// Set cell style
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderBottom(BorderStyle.DOUBLE);
style.setBottomBorderColor(IndexedColors.GREEN.getIndex());
style.setFillBackgroundColor(HSSFColor.LEMON_CHIFFON.index);
style.setFillPattern(FillPatternType.LESS_DOTS);
// Set font
XSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 30);
font.setFontName("Arial");
font.setItalic(true);
font.setColor(HSSFColor.BRIGHT_GREEN.index);
style.setFont(font);
cell.setCellStyle(style);
workbook.write(fos);
fos.close();
System.out.println("poi-excel.xlsx written successful.");
}
Parse Excel
@Test
public void parseExcel() throws IOException {
FileInputStream fis = new FileInputStream(new File("./poi-excel.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet spreadsheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = spreadsheet.rowIterator();
while(rowIterator.hasNext()) {
XSSFRow row = (XSSFRow) rowIterator.next();
XSSFCell cell = row.getStringCellValue(0);
String value = cell.toString();
System.out.println(value);
}
fis.close();
}
iTxt
dependencies
<dependencies>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.0.3</version>
</dependency>
</dependencies>
Create Pdf
@Test
public void createPdf() throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("/Users/admin/Desktop/itext-pdf.pdf"));
// Creating a Document object
Document doc = new Document(pdfDoc);
// Creating a table
Table table = new Table(2);
// Adding cell to the table
Cell cell = new Cell().add("Test");
cell.setBackgroundColor(Color.PINK);
cell.setBorderBottom(Border.NO_BORDER);
cell.setTextAlignment(TextAlignment.CENTER);
cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.addCell(cell);
// Support Chinese
PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", false);
table.setFont(font);
// Adding Table to document
doc.add(table);
// Image
Image img = new Image(ImageDataFactory.create("url"));
doc.add(img);
// Creating a Paragraph
Paragraph paragraph = new Paragraph("My hobby:");
doc.add(paragraph);
// Creating a List
List list = new List();
list.add("play games");
list.add("play water");
doc.add(list);
// Adding text to the document
Text text = new Text("Hello, this is a text.");
text.setFontColor(Color.RED);
Paragraph para = new Paragraph(text);
doc.add(para);
// Closing the document
doc.close();
System.out.println("Pdf created successfully..");
}
Download File
OutputStream os = response.getOutputStream();
response.setContentType("application/octet-stream");
response.setHeader("content-disposition", "attachment;filename=book.xls");
workbook.wirte(os);
os.flush();
os.close();
// axios发送请求
return request({
url: "/export",
method: "post",
responseType: 'blob' // 添加响应类型
})
// axios接收响应
const filename = rsp.headers["content-disposition"];
const blob = new Blob([rsp.data]);
var downloadElement = document.createElement("a");
var href = window.URL.createObjectURL(blob);
downloadElement.href = href;
downloadElement.download = decodeURIComponent(filename.split("filename=")[1]);
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
window.URL.revokeObjectURL(href);