根据textarea的输出更新excel文件
我的代码导入excel文件,然后把一个特定的列到一个textarea我想知道我将如何修改excel文件,如果我编辑了textarea编辑应保存在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(); } }
- 打开一个文件为
XSSFWorkbook
- 打开工作簿的
XSSFSheet
- 在表单中进行一些更改。 例如,更改
XSSFCell
的值 - 通过
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"));