1.所需依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
2.代码:xls转docx
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Iterator;
@RestController
public class XlsConversionController {
@RequestMapping("/uploadXlsConvert")
public ResponseEntity<byte[]> uploadXlsConvert(@RequestParam("file") MultipartFile uploadedFile) throws Exception {
InputStream fileInputStream = uploadedFile.getInputStream();
if (!uploadedFile.getOriginalFilename().endsWith(".xls")) {
throw new IllegalArgumentException("请您上传.xls格式文件");
}
Workbook workbook = new HSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
// 2. 创建 Word 文档
XWPFDocument document = new XWPFDocument();
// 3. 创建 Word 表格,大小与 Excel 表格相同
int numRows = sheet.getPhysicalNumberOfRows();
int numCols = sheet.getRow(0).getPhysicalNumberOfCells();
XWPFTable table = document.createTable(numRows, numCols);
// 4. 从 Excel 中读取数据并插入到 Word 表格中
Iterator<Row> rowIterator = sheet.iterator();
int rowIndex = 0;
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.iterator();
int colIndex = 0;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
XWPFTableCell tableCell = table.getRow(rowIndex).getCell(colIndex);
XWPFParagraph paragraph = tableCell.addParagraph();
XWPFRun run = paragraph.createRun();
run.setText(cell.toString());
colIndex++;
}
rowIndex++;
}
// 5. 将 Word 文档转换为 byte array (blob)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.write(outputStream);
byte[] wordBytes = outputStream.toByteArray();
outputStream.close();
// 6. 关闭 Excel 文件
fileInputStream.close();
// 7. Create HTTP headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "output.docx");
// 8. Return the Word document as a ResponseEntity
return ResponseEntity.ok()
.headers(headers)
.body(wordBytes);
}
}
3.代码:xls转docx
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
@RestController
public class XlsxConversionController {
@RequestMapping("/uploadXlsxConvert")
public ResponseEntity<byte[]> uploadXlsxConvert(@RequestParam("file") MultipartFile uploadedFile) throws Exception {
InputStream fileInputStream = uploadedFile.getInputStream();
if (!uploadedFile.getOriginalFilename().endsWith(".xlsx")) {
throw new IllegalArgumentException("请您上传.xlsx格式文件");
}
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
// 2. 创建 Word 文档
XWPFDocument document = new XWPFDocument();
// 3. 创建 Word 表格,大小与 Excel 表格相同
int numRows = sheet.getPhysicalNumberOfRows();
int numCols = sheet.getRow(0).getPhysicalNumberOfCells();
XWPFTable table = document.createTable(numRows, numCols);
// 4. 从 Excel 中读取数据并插入到 Word 表格中
Iterator<Row> rowIterator = sheet.iterator();
int rowIndex = 0;
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.iterator();
int colIndex = 0;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
XWPFTableCell tableCell = table.getRow(rowIndex).getCell(colIndex);
XWPFParagraph paragraph = tableCell.addParagraph();
XWPFRun run = paragraph.createRun();
run.setText(cell.toString());
colIndex++;
}
rowIndex++;
}
// 5. 将 Word 文档转换为 byte array (blob)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.write(outputStream);
byte[] wordBytes = outputStream.toByteArray();
outputStream.close();
// 6. 关闭 Excel 文件
fileInputStream.close();
// 7. Create HTTP headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "output.docx");
// 8. Return the Word document as a ResponseEntity
return ResponseEntity.ok()
.headers(headers)
.body(wordBytes);
}
}