猿问

使用 POI 下载的文件已损坏,并且不使用弹出窗口存储数据

  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());

        }


因此,在 ExcelWriter 文件中,按照指定文件应下载到指定的文件路径。浏览器中会生成一个弹出窗口,但打开的 Excel 文件已损坏,并且未存储硬编码数据。另一方面,数据位于指定位置的 Excel 文件中,不会出现在弹出窗口中,并且 EXCEL 文件处于兼容模式。


LEATH
浏览 233回答 1
1回答

呼如林

尝试这个:public void doGet(HttpServletRequest request, HttpServletResponse response)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws ServletException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExcelWriter excelWriter = new ExcelWriter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Book> listBook = excelWriter.getListBook();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; excelWriter.writeExcel(listBook, excelFilePath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Excel file written successfully");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String excelFilePath = "C:\\Users\\A7369241\\Desktop\\Temp.xls";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.setContentType("application/octet-stream");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.setHeader("Content-Disposition", "attachment;filename=temp.xls");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutputStream out = response.getOutputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try (FileInputStream in = new FileInputStream(file)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = new byte[4096];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((length = in.read(buffer)) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.write(buffer, 0, length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.flush();&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }如果您只想创建 excel,则无需使用响应。并且文件名应该是 xlsx 类型。这就是您收到兼容模式消息的原因。try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String excelFilePath = "C:\\Users\\A7369241\\Desktop\\Temp.xlsx";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExcelWriter excelWriter = new ExcelWriter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Book> listBook = excelWriter.getListBook();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; excelWriter.writeExcel(listBook, excelFilePath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Excel file written successfully");&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }如果您仍然有问题,则意味着您的 servlet 无法正常工作。只需尝试使用 main 方法和 main 方法类型创建类:String excelFilePath = "D:\\Temp.xls";Test excelWriter = new Test();List<Book> listBook = excelWriter.getListBook();try {&nbsp; &nbsp; excelWriter.writeExcel(listBook, excelFilePath);&nbsp; &nbsp; System.out.println("Excel file written successfully");} catch (IOException e) {&nbsp; &nbsp; e.printStackTrace();}
随时随地看视频慕课网APP

相关分类

Java
我要回答