import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.xssf.usermodel.XSSFWorkbook;
public class ExcelWriter extends HttpServlet {
private void writeExcel(List<Book> listBook, String excelFilePath)
throws IOException {
Workbook workbook = getWorkbook(excelFilePath);
Sheet sheet = workbook.createSheet();
int rowCount = 0;
for (Book aBook : listBook) {
Row row = sheet.createRow(++rowCount);
writeBook(aBook, row);
}
try (FileOutputStream outputStream = new FileOutputStream(new File(
excelFilePath))) {
workbook.write(outputStream);
}
}
private void writeBook(Book aBook, Row row) {
Cell cell = row.createCell(1);
cell.setCellValue(aBook.getTitle());
cell = row.createCell(2);
cell.setCellValue(aBook.getAuthor());
cell = row.createCell(3);
cell.setCellValue(aBook.getPrice());
}
private List<Book> getListBook() {
Book book1 = new Book("Head Java", "Anot Serria", 79);
Book book2 = new Book("Effective Java 1", "Bnot Bloch", 36);
Book book3 = new Book("Clean Code 1", "Cnot Martin", 42);
Book book4 = new Book("Thinking in Java 2", "D Eckel", 35);
List<Book> listBook = Arrays.asList(book1, book2, book3, book4);
return listBook;
}
private Workbook getWorkbook(String excelFilePath) throws IOException {
Workbook workbook = null;
if (excelFilePath.endsWith("xlsx")) {
workbook = new XSSFWorkbook();
} else if (excelFilePath.endsWith("xls")) {
workbook = new HSSFWorkbook();
} else {
throw new IllegalArgumentException(
"The specified file is not Excel file");
}
return workbook;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String excelFilePath = "C:\\Users\\A7369241\\Desktop\\Temp.xls";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="
+ excelFilePath);
ExcelWriter excelWriter = new ExcelWriter();
List<Book> listBook = excelWriter.getListBook();
excelWriter.writeExcel(listBook, excelFilePath);
System.out.println("Excel file written successfully");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public class Book {
private String title,author;
private float price;
public Book(String title, String author, float price) {
super();
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
因此,在ExcelWriter文件中,应按指定的路径在指定的文件路径下载该文件。浏览器中会生成一个弹出窗口,但是打开的Excel文件已损坏,并且不存储硬编码数据。另一方面,数据位于Excel文件中的指定位置,该位置不会显示在弹出窗口中,并且EXCEL文件处于兼容模式。
有人可以帮我吗?
答案 0 :(得分:2)
尝试一下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
ExcelWriter excelWriter = new ExcelWriter();
List<Book> listBook = excelWriter.getListBook();
excelWriter.writeExcel(listBook, excelFilePath);
System.out.println("Excel file written successfully");
String excelFilePath = "C:\\Users\\A7369241\\Desktop\\Temp.xls";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=temp.xls");
OutputStream out = response.getOutputStream();
try (FileInputStream in = new FileInputStream(file)) {
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
}
out.flush();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
如果您只想创建excel,则无需使用响应。并且文件名应为xlsx类型。这就是为什么您收到兼容模式消息的原因。
try {
String excelFilePath = "C:\\Users\\A7369241\\Desktop\\Temp.xlsx";
ExcelWriter excelWriter = new ExcelWriter();
List<Book> listBook = excelWriter.getListBook();
excelWriter.writeExcel(listBook, excelFilePath);
System.out.println("Excel file written successfully");
} catch (Exception e) {
System.out.println(e.getMessage());
}
如果仍然存在问题,则意味着servlet无法正常工作。只需尝试使用main方法和main方法类型创建类:
String excelFilePath = "D:\\Temp.xls";
Test excelWriter = new Test();
List<Book> listBook = excelWriter.getListBook();
try {
excelWriter.writeExcel(listBook, excelFilePath);
System.out.println("Excel file written successfully");
} catch (IOException e) {
e.printStackTrace();
}