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

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

為什么我不能阻止 Apache POI 更改源文件?

為什么我不能阻止 Apache POI 更改源文件?

一只斗牛犬 2023-02-23 18:05:51
我正在使用 Apache POI 工作簿在 Java 中打開一個 Excel 文件(源),更改一組特定單元格中的數(shù)據(jù),將工作簿保存到一個單獨的文件,然后關閉工作簿(因為文檔說明要關閉工作簿,甚至如果它是只讀的)。POI 每次都會更改源 Excel 文件中的數(shù)據(jù)。根據(jù) POI 文檔的建議,我嘗試了幾種不同的方法來防止這種情況發(fā)生,但這些方法都失敗了。這里有兩種嘗試在理論上應該有效,但沒有。嘗試 1 - 將源文件設置為只讀File file = new File("{path-to-existing-source-file}");file.setReadOnly();Workbook workbook = WorkbookFactory.create(file); // throws a FileNotFoundException“訪問被拒絕”的AFileNotFoundException拋出在WorkbookFactory.create(file):java.io.FileNotFoundException: {path-to-source-file-that-exists} (Access is denied)at java.io.RandomAccessFile.open0(Native Method)at java.io.RandomAccessFile.open(RandomAccessFile.java:316)at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)at org.apache.poi.poifs.nio.FileBackedDataSource.newSrcFile(FileBackedDataSource.java:158)at org.apache.poi.poifs.nio.FileBackedDataSource.<init>(FileBackedDataSource.java:60)at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:224)at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:172)at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:298)at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:271)at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)at com.stackoverflow.MyClass(MyClass.java:71)源文件存在,并且有效只讀。嘗試 2 - 使用允許顯式設置只讀的 POI API 構造函數(shù)File file = new File("{path-to-existing-source-file}");Workbook workbook = WorkbookFactory.create(file, null, true);  // true is read-only// dataBean is just a container bean with the appropriate reference valuesSheet sheet = workbook.getSheet(dataBean.getSheetName());Row row = sheet.getRow(dataBean.getRowNumber());Cell cell = row.getCell(dataBean.getColumnNumber());cell.setCellValue(dataBean.getValue());“不允許操作,文檔以只讀模式打開!”。當然設置為只讀;我不想寫入源,我只想將所有數(shù)據(jù)發(fā)送到新目標。使用 POI 時,我可以設置或更改什么以不改變來源?我們目前的解決方法是創(chuàng)建一個重復的源文件,但這不是一個好的解決方案。
查看完整描述

4 回答

?
胡說叔叔

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

我遇到了同樣的問題并通過使用 aFileInputStream而不是 a解決了它File

Workbook workbook = WorkbookFactory.create(file);

變成:

Workbook workbook = WorkbookFactory.create(new FileInputStream(file));


查看完整回答
反對 回復 2023-02-23
?
DIEA

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

您需要有兩本工作簿,一本用于獲?。ㄗx取)數(shù)據(jù),另一本用于寫入數(shù)據(jù)。


伙計,這就是我?guī)讉€月前的做法,請注意我在第二個工作簿 (hssfWorkbookNew) 上使用 .write(),而不是我用來讀取數(shù)據(jù)的那個,請仔細閱讀。此代碼僅用于獲取 XLS excel 的第一張紙并將其復制到新文件。


// this method generates a new excelFile based on the excelFile he receives


public void generarXLS(File excelFile, File excelNewFile) {

        InputStream excelStream = null;

        OutputStream excelNewOutputStream = null;

        try {

            excelStream = new FileInputStream(excelFile);

            excelNewOutputStream = new FileOutputStream(excelNewFile);

            // Representation of highest level of excel sheet.

            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(excelStream);

            HSSFWorkbook hssfWorkbookNew = new HSSFWorkbook();


            // Chose the sheet that we pass as parameter.

            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);


            // Create new sheet we are gonna use.

            HSSFSheet hssfSheetNew = hssfWorkbookNew.createSheet("Copy-Copia");


            // Create new sheet where we will copy the data


            // Object that allow us to read a row from the sheet and extract the data from the cells

            HSSFRow hssfRow;

            HSSFRow hssfRowNew; // for hssfSheetNew

            // Initialize the object that reads value of cell

            HSSFCell cellNew;

            // Get number of rows of the sheet

            int rows = hssfSheet.getLastRowNum();

            String cellValue;


            // Style of the cell border, color background and pattern (fill pattern) used.

            CellStyle style = hssfWorkbookNew.createCellStyle();

            // Definition of the font of the cell.


            // Iterate trhough all rows to get the cells and copy them to the new sheet

            for (Row row : hssfSheet) {

                hssfRowNew = hssfSheetNew.createRow(row.getRowNum());


                if (row.getRowNum() > 999999) {

                    break;

                }


                for (Cell cell : row) {


                    cellValue = (cell.getCellType() == CellType.STRING) ? cell.getStringCellValue()

                            : (cell.getCellType() == CellType.NUMERIC) ? "" + cell.getNumericCellValue()

                                    : (cell.getCellType() == CellType.BOOLEAN) ? "" + cell.getBooleanCellValue()

                                            : (cell.getCellType() == CellType.BLANK) ? ""

                                                    : (cell.getCellType() == CellType.FORMULA) ? "FORMULA"

                                                            : (cell.getCellType() == CellType.ERROR) ? "ERROR" : "";


                    cellNew = hssfRowNew.createCell(cell.getColumnIndex(), CellType.STRING);

                    cellNew.setCellValue(cellValue);


                }

            }

            // NOTICE how I write to the new workbook

            hssfWorkbookNew.write(excelNewOutputStream);

            hssfWorkbook.close();

            hssfWorkbookNew.close();

            excelNewOutputStream.close();


            JOptionPane.showMessageDialog(null, Constantes.MSG_EXITO, "Informacion", 1);


        } catch (FileNotFoundException fileNotFoundException) {

            JOptionPane.showMessageDialog(null, "file not found", "Error", 0);


        } catch (IOException ex) {

            JOptionPane.showMessageDialog(null, "Error processing the file", "Error", 0);


        } finally {

            try {

                excelStream.close();

            } catch (IOException ex) {

                System.out.println("Error processing the file after closing it): " + ex);

            }

        }

    }


查看完整回答
反對 回復 2023-02-23
?
慕妹3242003

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

我必須處理 XSSF 和 HSSF;這是它是如何完成的:


void handle(File inFile, File outFile) throws IOException {    


    Workbook workbook = WorkbookFactory.create(inFile);

    workbook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);  // LINE NOT REQUIRED


    if (workbook instanceof XSSFWorkbook) {


        handleXSSF((XSSFWorkbook) workbook, outFile);


    } else if (workbook instanceof HSSFWorkbook) {


        handleHSSF((HSSFWorkbook) workbook, outFile);


    } else {


        throw new IOException("Unrecognized Workbook Type " + workbook.getClass().getName());

    }

}


void handleHSSF(HSSFWorkbook hWorkbook, File outFile) throws IOException {


    FileOutputStream fos = null;


    try {


        fos = new FileOutputStream(outFile);    

        hWorkbook.write(fos);

        fos.close();


    } finally {


        try { 


            hWorkbook.close();


        } catch (Exception ignore) {}

    }

}


void handleXSSF(XSSFWorkbook xWorkbook, File outFile) throws IOException {


    SXSSFWorkbook sWorkbook = new SXSSFWorkbook(xWorkbook, 100);


    FileOutputStream fos = null;


    try {


        fos = new FileOutputStream(outFile);    

        sWorkbook.write(fos);

        fos.close();


    } finally {


        try { 


            sWorkbook.close();


        } catch (Exception ignore) {}


        try { 


            sWorkbook.dispose();


        } catch (Exception ignore) {}


        try { 


            xWorkbook.close();


        } catch (Exception ignore) {}

    }

}


查看完整回答
反對 回復 2023-02-23
?
慕尼黑8549860

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

也許你也可以只使用創(chuàng)建簽名

Workbook workbook = WorkbookFactory.create(new File("//server/path/file.ext"), null, true);

要求POI以只讀方式打開電子表格?


查看完整回答
反對 回復 2023-02-23
  • 4 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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