Java修改Excel某一列内容

Java修改Excel某一列内容

public class Test {
    public static void main(String[] args) throws IOException {
        IDEA idea = new IDEA();
        idea.getKey("jx123456");
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\2.xlsx");
            // 根据输入流创建工作簿
            Workbook workbook = new XSSFWorkbook(inputStream);
            // 获取表
            Sheet sheet = workbook.getSheet("SQL Results");
            int lastRowNum = sheet.getLastRowNum();
            System.out.println(lastRowNum);
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (null == row) {
                    continue;
                } else {
                    Cell cell = row.getCell(1);
                    if (null == cell) {
                        continue;
                    } else {
                        String stringCellValue = cell.getStringCellValue();
                        System.out.println(stringCellValue);

                        if (stringCellValue.length() == 64) {
                            String desString = idea.getDesString(stringCellValue);
                            System.out.println(desString);
                            cell.setCellValue(desString);
                        }
                    }
                }
            }
            outputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\3.xlsx");
            workbook.write(outputStream);
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
            outputStream.close();
        }
    }
}
如果你想要修改一列的格式,可以通过使用 JavaPOI 库来实现。 以下是一个示例代码,它将读取一个 Excel 文件,并将其中的某一列的格式更改为数字: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormatter; 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 ExcelModifyColumn { public static void main(String[] args) throws IOException { // 读取 Excel 文件 String path = "path/to/excel.xlsx"; Workbook workbook = new XSSFWorkbook(new File(path)); Sheet sheet = workbook.getSheetAt(0); // 获取要修改的列 int columnToModify = 2; // 假设要修改第3列 DataFormatter dataFormatter = new DataFormatter(); for (Row row : sheet) { Cell cell = row.getCell(columnToModify); if (cell != null) { String cellValue = dataFormatter.formatCellValue(cell); try { // 将单元格的值转换为数字,如果转换不成功则抛出异常 double value = Double.parseDouble(cellValue); cell.setCellValue(value); CellStyle style = workbook.createCellStyle(); style.setDataFormat(workbook.createDataFormat().getFormat("#,##0.00")); cell.setCellStyle(style); } catch (NumberFormatException ex) { // 如果转换不成功,则不做任何处理 } } } // 保存修改后的 Excel 文件 FileOutputStream outputStream = new FileOutputStream(path); workbook.write(outputStream); workbook.close(); outputStream.close(); System.out.println("Excel 文件已经修改完毕。"); } } ``` 在上面的代码中,我们首先使用 POI 库读取了一个 Excel 文件,然后遍历其中的每一行,并获取其中要修改的列。我们使用 DataFormatter 类来将单元格的值转换为字符串,并尝试将其转换为数字。如果转换成功,我们就将单元格的值设置为转换后的数字,并为该单元格设置一个格式样式,以确保它以数字格式显示。最后,我们将修改后的 Excel 文件保存到原文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值