IF function中的 currentFormula = "IF(A1=H1,I1,J1)";及currentFormula = "=IF(A2=H2,I2,J2))";没有运行,考虑过应该是加载文档的问题。
回复修改后的代码,一经测试成功必采。
直接上问题代码:
import com.spire.xls.*;
public class Test {
public static void main(String args[]){
//加载文档
Workbook wb = new Workbook();
wb.loadFromFile("原始数据.xlsx");
//获取第一张工作表
Worksheet sheet = wb.getWorksheets().get(0);
//Declare two variables: currentRow、currentFormula
int currentRow = 1;
String currentFormula = null;
//Set the column width
sheet.setColumnWidth(1, 32);
sheet.setColumnWidth(2, 16);
//Write test data into cells
sheet.getCellRange(currentRow,1).setValue("AI.data");
sheet.getCellRange(currentRow,2).setNumberValue(1);
sheet.getCellRange(currentRow,3).setNumberValue(2);
//Write text in cells
currentRow += 2;
sheet.getCellRange(currentRow,1).setValue("Formulas or functions:") ; ;
sheet.getCellRange(currentRow,2).setValue("result:");
//Format cells
CellRange range = sheet.getCellRange(currentRow,1,currentRow,2);
range.getStyle().getFont().isBold(true);
range.getStyle().setKnownColor(ExcelColors.BlueGray);
range.getStyle().setFillPattern(ExcelPatternType.Solid);
range.getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeBottom).setLineStyle(LineStyleType.Medium);
//IF function
currentFormula = "IF(A1=H1,I1,J1)";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//IF function
currentFormula = "=IF(A2=H2,I2,J2))";
sheet.getCellRange(++currentRow,1).setText(currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//保存文档
wb.saveToFile("system.xlsx",ExcelVersion.Version2013);
}
}