gpt4 book ai didi

java - 根据textarea的输出更新excel文件

转载 作者:行者123 更新时间:2023-12-01 10:50:42 25 4
gpt4 key购买 nike

我的代码导入 Excel 文件,然后将特定列放入文本区域我想知道如果我在文本区域上编辑了一些内容,那么我将如何修改Excel文件,该编辑内容应该保存在Excel文件中

String excelFilePath = "sample.xlsx";
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(new File(excelFilePath));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

Workbook workbook = null;
try {
workbook = new XSSFWorkbook(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
Sheet firstSheet = workbook.getSheetAt(0);

Iterator<Row> iterator = firstSheet.iterator();

while (iterator.hasNext()) {
Row nextRow = iterator.next();

Iterator<Cell> cellIterator = nextRow.cellIterator();
Iterator<Cell> scellIterator = nextRow.cellIterator();

cellIterator.next();
scellIterator.next();
scellIterator.next();
Cell topicsCell = cellIterator.next();
Cell topicSentimentCell =scellIterator.next();

String cellContents = topicsCell.getStringCellValue();
String scellContents = topicSentimentCell.getStringCellValue();

String[] topics = cellContents.split(";");
String[] topicSentiment = scellContents.split(";");

ArrayList<String> tpc = new ArrayList<>();
ArrayList<String> topicsents = new ArrayList<>();
for(int i = 0; i < topics.length; i++) {
topics[i] = topics[i].trim();
tpc.add(topics[i]);

for (int indx = 0; indx < tpc.size(); indx++) {
textArea.append(tpc.get(indx)+"\n");
}

}

for(int si = 0; si < topicSentiment.length; si++) {
topicSentiment[si] = topicSentiment[si].trim();
topicsents.add(topicSentiment[si]);

for (int index = 0; index < topicsents.size(); index++) {
// textArea.append(topicsents.get(index)+"\n");
System.out.print(topicsents+"\n");


}

}


}


try {
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}

}

最佳答案

  1. 将文件打开为 XSSFWorkbook
  2. 打开工作簿的 XSSFSheet
  3. 在工作表中进行一些更改。例如更改 XSSFCell
  4. 的值
  5. 通过workbook.write()保存更改
<小时/>

示例

// Open workbook and sheet
final XSSFWorkbook workbook = new XSSFWorkbook(new File("filename.xlsx"));
final XSSFSheet sheet = workbook.getSheetAt(0);

// Iterate rows
final int firstRowNumber = sheet.getFirstRowNum();
final int lastRowNumber = sheet.getLastRowNum();
for (int rowNumber = firstRowNumber; rowNumber < lastRowNumber; rowNumber++ ) {
final XSSFRow row = sheet.getRow(rowNumber);

if (row == null) continue;

// Iterate columns
final int firstColumnNumber = row.getFirstCellNum();
final int lastColumnNumber = row.getLastCellNum();
for (int columnNumber = firstColumnNumber; columnNumber < lastColumnNumber; columnNumber++ ) {
final XSSFCell cell = row.getCell(firstColumnNumber);

if (cell == null) continue;

// Make some changes
cell.setCellValue("new Value");
}
}

// Save changes
workbook.write(new FileOutputStream("newFilename.xlsx"));

关于java - 根据textarea的输出更新excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33933926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com