我一直在尝试从this site自定义代码,以便将excel工作簿中的表格转换为csv文件。我在上面的片段中做了一些小的修改,并且只给这个片段添加了一个标签,因为它抛出了一个不同的错误。现在,我在for循环中的第一个"=“处得到了一个意外的令牌错误:
.groovy: 21: unexpected token: = @ line 21, column 16.
for (int r = 0, rn = sheet.getLastRowNum() ; r <= rn ; r++) {
^
1 error
整个脚本包含在下面。我尝试重新输入代码以查找任何语法错误,但我担心这可能只是groovy解释这段代码的方式上的一种误解。
#!/usr/bin/env groovy
@Grab(group='org.apache.poi', module='poi', version='4.1.0')
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.apache.poi.ss.usermodel.DataFormatter
Workbook wb = new XSSFWorkbook(new File(this.args));
int sheetNo = Integer.parseInt(args[index++]);
FormulaEvaluator fe = null;
if ( index < args.length ) {
fe = wb.getCreationHelper().createFormulaEvaluator();
}
DataFormatter formatter = new DataFormatter();
PrintStream out = new PrintStream(new FileOutputStream(csvFile), true, "UTF-8");
byte[] bom = [(byte)0xEF, (byte)0xBB, (byte)0xBF];
out.write(bom);
label:{
Sheet sheet = wb.getSheetAt(sheetNo);
for (int r = 0, rn = sheet.getLastRowNum() ; r <= rn ; r++) {
Row row = sheet.getRow(r);
if ( row == null ) { out.println(','); continue; }
boolean firstCell = true;
for (int c = 0, cn = row.getLastCellNum() ; c < cn ; c++) {
Cell cell = row.getCell(c, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
if ( ! firstCell ) out.print(',');
if ( cell != null ) {
if ( fe != null ) cell = fe.evaluateInCell(cell);
String value = formatter.formatCellValue(cell);
if ( cell.getCellTypeEnum() == CellType.FORMULA ) {
value = "=" + value;
}
out.print(encodeValue(value));
}
firstCell = false;
}
out.println();
}
}
发布于 2019-06-07 02:22:23
只需将其分成两个语句:
int cn = row.getLastCellNum()
for (int c = 0 ; c < cn ; c++) {
...
https://stackoverflow.com/questions/56482688
复制相似问题