随POI下载的文件已损坏,且未将数据存储在弹出窗口中 导入java.io.File; 导入java.io.FileOutputStream; 导入java.io.IOException; 导入java.util.array; 导入java.util.List; 导入javax.servlet.ServletException; 导入javax.servlet.http.HttpServlet; 导入javax.servlet.http.HttpServletRequest; 导入javax.servlet.http.HttpServletResponse; 导入org.apache.poi.hssf.usermodel.HSSFWorkbook; 导入org.apache.poi.ss.usermodel.Cell; 导入org.apache.poi.ss.usermodel.Row; 导入org.apache.poi.ss.usermodel.Sheet; 导入org.apache.poi.ss.usermodel.工作簿; 导入org.apache.poi.xssf.usermodel.xssf工作簿; 公共类ExcelWriter扩展了HttpServlet{ 私有void writeExcel(列表簿、字符串excelFilePath) 抛出IOException{ 工作簿=获取工作簿(excelFilePath); 工作表=工作簿.createSheet(); int rowCount=0; 对于(图书目录:listBook){ Row Row=sheet.createRow(++rowCount); 书写本(aBook,row); } try(FileOutputStream outputStream=newfileoutputstream(新文件( excelFilePath(文件路径){ workbook.write(outputStream); } } 私有无效书写本(书籍A、行){ 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()); } 私有列表getListBook(){ Book book1=新书(“头Java”,“Anot Serria”,79); Book book2=新书(“有效Java 1”,“Bnot Bloch”,36); Book book3=新书(“清洁代码1”,“Cnot Martin”,42); book4=新书(“用Java 2思考”,“D Eckel”,35); List listBook=Arrays.asList(book1、book2、book3、book4); 返回目录; } 私有工作簿getWorkbook(字符串excelFilePath)引发IOException{ 工作簿=空; if(excelFilePath.endsWith(“xlsx”)){ 工作簿=新XSSFWorkbook(); }else if(excelFilePath.endsWith(“xls”)){ 工作簿=新的HSSF工作簿(); }否则{ 抛出新的IllegalArgumentException( “指定的文件不是Excel文件”); } 返回工作簿; } public void doGet(HttpServletRequest请求、HttpServletResponse响应) 抛出ServletException、IOException{ 试一试{ String excelFilePath=“C:\\Users\\A7369241\\Desktop\\Temp.xls”; setContentType(“应用程序/八位字节流”); setHeader(“内容处置”、“附件;文件名=” +excelFilePath); ExcelWriter ExcelWriter=新的ExcelWriter(); List listBook=excelWriter.getListBook(); writeExcel(listBook,excelFilePath); System.out.println(“Excel文件写入成功”); }捕获(例外e){ System.out.println(e.getMessage()); } } } 公共课堂用书{ 私人字符串标题,作者; 私人浮动价格; 公共书籍(字符串标题、字符串作者、浮动价格){ 超级(); this.title=标题; this.author=作者; 这个价格=价格; } 公共字符串getTitle(){ 返回标题; } 公共无效集合标题(字符串标题){ this.title=标题; } 公共字符串getAuthor(){ 返回作者; } 公共void setAuthor(字符串编写器){ this.author=作者; } 公开发行价格(){ 退货价格; } 公共定价(浮动价格){ 这个价格=价格; } }
因此,在ExcelWriter文件中,应按照指定的文件路径下载该文件。浏览器中会生成一个弹出窗口,但打开的Excel文件已损坏且未存储硬编码数据。另一方面,数据位于Excel文件中的指定位置,该位置不会出现在弹出窗口中,并且Excel文件处于兼容模式 有人能帮我吗。试试这个:随POI下载的文件已损坏,且未将数据存储在弹出窗口中 导入java.io.File; 导入java.io.FileOutputStream; 导入java.io.IOException; 导入java.util.array; 导入java.util.List; 导入javax.servlet.ServletException; 导入javax.servlet.http.HttpServlet; 导入javax.servlet.http.HttpServletRequest; 导入javax.servlet.http.HttpServletResponse; 导入org.apache.poi.hssf.usermodel.HSSFWorkbook; 导入org.apache.poi.ss.usermodel.Cell; 导入org.apache.poi.ss.usermodel.Row; 导入org.apache.poi.ss.usermodel.Sheet; 导入org.apache.poi.ss.usermodel.工作簿; 导入org.apache.poi.xssf.usermodel.xssf工作簿; 公共类ExcelWriter扩展了HttpServlet{ 私有void writeExcel(列表簿、字符串excelFilePath) 抛出IOException{ 工作簿=获取工作簿(excelFilePath); 工作表=工作簿.createSheet(); int rowCount=0; 对于(图书目录:listBook){ Row Row=sheet.createRow(++rowCount); 书写本(aBook,row); } try(FileOutputStream outputStream=newfileoutputstream(新文件( excelFilePath(文件路径){ workbook.write(outputStream); } } 私有无效书写本(书籍A、行){ 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()); } 私有列表getListBook(){ Book book1=新书(“头Java”,“Anot Serria”,79); Book book2=新书(“有效Java 1”,“Bnot Bloch”,36); Book book3=新书(“清洁代码1”,“Cnot Martin”,42); book4=新书(“用Java 2思考”,“D Eckel”,35); List listBook=Arrays.asList(book1、book2、book3、book4); 返回目录; } 私有工作簿getWorkbook(字符串excelFilePath)引发IOException{ 工作簿=空; if(excelFilePath.endsWith(“xlsx”)){ 工作簿=新XSSFWorkbook(); }else if(excelFilePath.endsWith(“xls”)){ 工作簿=新的HSSF工作簿(); }否则{ 抛出新的IllegalArgumentException( “指定的文件不是Excel文件”); } 返回工作簿; } public void doGet(HttpServletRequest请求、HttpServletResponse响应) 抛出ServletException、IOException{ 试一试{ String excelFilePath=“C:\\Users\\A7369241\\Desktop\\Temp.xls”; setContentType(“应用程序/八位字节流”); setHeader(“内容处置”、“附件;文件名=” +excelFilePath); ExcelWriter ExcelWriter=新的ExcelWriter(); List listBook=excelWriter.getListBook(); writeExcel(listBook,excelFilePath); System.out.println(“Excel文件写入成功”); }捕获(例外e){ System.out.println(e.getMessage()); } } } 公共课堂用书{ 私人字符串标题,作者; 私人浮动价格; 公共书籍(字符串标题、字符串作者、浮动价格){ 超级(); this.title=标题; this.author=作者; 这个价格=价格; } 公共字符串getTitle(){ 返回标题; } 公共无效集合标题(字符串标题){ this.title=标题; } 公共字符串getAuthor(){ 返回作者; } 公共void setAuthor(字符串编写器){ this.author=作者; } 公开发行价格(){ 退货价格; } 公共定价(浮动价格){ 这个价格=价格; } },java,html,servlets,apache-poi,Java,Html,Servlets,Apache Poi,因此,在ExcelWriter文件中,应按照指定的文件路径下载该文件。浏览器中会生成一个弹出窗口,但打开的Excel文件已损坏且未存储硬编码数据。另一方面,数据位于Excel文件中的指定位置,该位置不会出现在弹出窗口中,并且Excel文件处于兼容模式 有人能帮我吗。试试这个: import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.u
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;
}
}
public void doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出ServletException、IOException{
试一试{
ExcelWriter ExcelWriter=新的ExcelWriter();
List listBook=excelWriter.getListBook();
writeExcel(listBook,excelFilePath);
System.out.println(“Excel文件写入成功”);
String excelFilePath=“C:\\Users\\A7369241\\Desktop\\Temp.xls”;
setContentType(“应用程序/八位字节流”);
setHeader(“内容处置”、“附件;文件名=temp.xls”);
OutputStream out=response.getOutputStream();
try(FileInputStream in=newfileinputstream(文件)){
字节[]缓冲区=新字节[4096];
整数长度;
而((长度=in.read(缓冲区))>0){
out.write(缓冲区,0,长度);
}
}
out.flush();
}捕获(例外e){
System.out.println(e.getMessage());
}
}
如果您只想创建excel,则无需使用响应。文件名应为xlsx类型。这就是为什么你有兼容模式的信息
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());
}
}
试试看{
字符串excelFilePath=“C:\\Users\\A7369241\\Desktop\\Temp.xlsx”;
ExcelWriter ExcelWriter=新的ExcelWriter();
List listBook=excelWriter.getListBook();
E
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());
}
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();
}