62,628
社区成员
发帖
与我相关
我的任务
分享
这个计算结果只能拿到一个double类型的值,1.0对应星期天,2.0对应星期一,以此类推,楼主直接在后头用switch进行选择了,大哥们有没有更好点的办法啊
就是取不出来咯
/**
* 得到Excel,并解析内容 对2007及以上版本 使用XSSF解析
* @param file
* @throws FileNotFoundException
* @throws IOException
* @throws InvalidFormatException
*/
@Test
public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException {
InputStream ins = null;
Workbook wb = null;
ins=new FileInputStream(new File(file));
wb = WorkbookFactory.create(ins);
ins.close();
FormulaEvaluator formulaEvaluator=new XSSFFormulaEvaluator((XSSFWorkbook) wb);
//3.得到Excel工作表对象
Sheet sheet = wb.getSheetAt(0);
//总行数
int trLength = sheet.getLastRowNum();
//4.得到Excel工作表的行
Row row = sheet.getRow(0);
//总列数
int tdLength = row.getLastCellNum();
//5.得到Excel工作表指定行的单元格
Cell cell = row.getCell((short)1);
//6.得到单元格样式
CellStyle cellStyle = cell.getCellStyle();
for(int i=1;i<=trLength;i++){
//得到Excel工作表的行
Row row1 = sheet.getRow(i);
for(int j=0;j<tdLength;j++){
//得到Excel工作表指定行的单元格
Cell cell1 = row1.getCell(j);
/**
* 为了处理:Excel异常Cannot get a text value from a numeric cell
* 将所有列中的内容都设置成String类型格式
*/
if(cell1!=null){
cell1.setCellType(XSSFCell.CELL_TYPE_FORMULA);
cell1.setCellFormula("WEEKDAY(C1)");
System.out.print( Week.getName(formulaEvaluator.evaluate(cell1).getNumberValue()));
}
//获得每一列中的值
System.out.print(cell1+" ");
}
System.out.println();
}
//将修改后的数据保存
OutputStream out = new FileOutputStream(file);
wb.write(out);
}
public enum Week {
Mon("星期一", 2.0), Tue("星期二", 3.0), Wed("星期三", 4.0), Thu("星期四", 5.0), Fri("星期五", 6.0), Sat("星期六", 7.0), Sun("星期日", 1.0);
// 成员变量
private String dayName;
private double index;
// 构造方法
private Week(String dayName, double index) {
this.dayName = dayName;
this.index = index;
}
// 普通方法
public static String getName(double index) {
for (Week w : Week.values()) {
if (w.getIndex() == index) {
return w.dayName;
}
}
return null;
}
public String getDayName() {
return dayName;
}
public void setDayName(String dayName) {
this.dayName = dayName;
}
public double getIndex() {
return index;
}
public void setIndex(double index) {
this.index = index;
}
}

/**
* 得到Excel,并解析内容 对2007及以上版本 使用XSSF解析
* @param file
* @throws FileNotFoundException
* @throws IOException
* @throws InvalidFormatException
*/
@Test
public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException {
InputStream ins = null;
Workbook wb = null;
ins=new FileInputStream(new File(file));
wb = WorkbookFactory.create(ins);
ins.close();
FormulaEvaluator formulaEvaluator=new XSSFFormulaEvaluator((XSSFWorkbook) wb);
//3.得到Excel工作表对象
Sheet sheet = wb.getSheetAt(0);
//总行数
int trLength = sheet.getLastRowNum();
//4.得到Excel工作表的行
Row row = sheet.getRow(0);
//总列数
int tdLength = row.getLastCellNum();
//5.得到Excel工作表指定行的单元格
Cell cell = row.getCell((short)1);
//6.得到单元格样式
CellStyle cellStyle = cell.getCellStyle();
for(int i=1;i<=trLength;i++){
//得到Excel工作表的行
Row row1 = sheet.getRow(i);
for(int j=0;j<tdLength;j++){
//得到Excel工作表指定行的单元格
Cell cell1 = row1.getCell(j);
/**
* 为了处理:Excel异常Cannot get a text value from a numeric cell
* 将所有列中的内容都设置成String类型格式
*/
if(cell1!=null){
cell1.setCellType(XSSFCell.CELL_TYPE_FORMULA);
cell1.setCellFormula("WEEKDAY(C1)");
System.out.print( formulaEvaluator.evaluate(cell1).getNumberValue());
}
//获得每一列中的值
System.out.print(cell1+" ");
}
System.out.println();
}
//将修改后的数据保存
OutputStream out = new FileOutputStream(file);
wb.write(out);
}