为什么不能使用Apache POI在此excel文件中写入数据?

问题描述

我正在执行一些任务,需要打开excel文件并在该文件中写入一些数据。我正在为此使用apache POI。我基本上可以通过创建不同的文件来写一些其他文件。但是有了这个文件,我无法在其中写入数据。

你能帮帮我吗。该文件有什么问题?它是锁定还是冻结? 这是我的代码

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.hssfSheet;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetProtection;

public class AAA2 {

    public static void main(String[] args) {
        AAA2 aaa2 = new AAA2();
        aaa2.xlsOps();
    }

    private void xlsOps() {
        String filePath="C:\\dev\\excel_file.xls";
        try (InputStream inp = new FileInputStream(filePath)) {
            
                Workbook wb = WorkbookFactory.create(inp);
                hssfSheet sheet = (hssfSheet) wb.getSheetAt(0);
               // XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0);
                
                CellStyle unlockStyle=wb.createCellStyle();
                unlockStyle.setLocked(false);   
                
                //Row part
                Row row = sheet.getRow(6);
                row.setRowStyle(unlockStyle);
                System.out.println("Row:"+row);

                //Row row = null;
//              if(row == null) {
//                  row =sheet.createRow(6);
//                  System.out.println("Row Lock: "+row.getRowStyle());
//              }
                // Cell part.
                Cell cell = row.getCell(5);
                System.out.println(cell);
                if(cell == null) {
                    cell = row.createCell(5);
                    System.out.println("After creation lockVal: "+cell.getCellStyle().getLocked()); 
                    cell.setCellStyle(unlockStyle);
                }
                
                System.out.println("Update the lock as false: "+cell.getCellStyle().getLocked());
                cell.setCellValue("TEST 1223435");
                System.out.println(cell);
                
                // Write the output to a file
                try (OutputStream fileOut = new FileOutputStream("C:\\dev\\x-new22.xls")) {
                    wb.write(fileOut);
                }
                System.out.println("sheet created.");
            } catch (FileNotFoundException e) {
                e.printstacktrace();
            } catch (IOException e) {
                e.printstacktrace();
            } catch (InvalidFormatException e) {
                e.printstacktrace();
            }
    }
}

我做错了什么?行或单元格在给定的excel中是固定的吗? 这是文件enter link description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...