第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 POI 下載的文件已損壞,并且不使用彈出窗口存儲數(shù)據(jù)

使用 POI 下載的文件已損壞,并且不使用彈出窗口存儲數(shù)據(jù)

LEATH 2022-03-10 21:39:25
  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 文件中,按照指定文件應(yīng)下載到指定的文件路徑。瀏覽器中會生成一個彈出窗口,但打開的 Excel 文件已損壞,并且未存儲硬編碼數(shù)據(jù)。另一方面,數(shù)據(jù)位于指定位置的 Excel 文件中,不會出現(xiàn)在彈出窗口中,并且 EXCEL 文件處于兼容模式。
查看完整描述

1 回答

?
呼如林

TA貢獻1798條經(jīng)驗 獲得超3個贊

嘗試這個:


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

        }

    }

如果您只想創(chuàng)建 excel,則無需使用響應(yīng)。并且文件名應(yīng)該是 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 方法類型創(chuàng)建類:


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

}


查看完整回答
反對 回復(fù) 2022-03-10
  • 1 回答
  • 0 關(guān)注
  • 264 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號