使用Apache POI处理EXCEL数据

Apache POI是一个处理EXCEL类型数据的java package。数据的后缀可以是a.xls或者a.xlsx。
接下来是一个使用Apache POI处理EXCEL数据的例子。

package cn.biz.nonsense.optimus.AAA;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class DataPreprocess {
    static XSSFRow row;
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception 
    {
       FileInputStream fis = new FileInputStream(
       new File(DataPreprocess.class.getResource("/").getPath()+"file.xlsx"));
       XSSFWorkbook workbook = new XSSFWorkbook(fis);
       XSSFSheet spreadsheet = workbook.getSheetAt(0);
       Iterator < Row > rowIterator = spreadsheet.iterator();
       while (rowIterator.hasNext()) 
       {
          row = (XSSFRow) rowIterator.next();
          Iterator < Cell > cellIterator = row.cellIterator();
          while ( cellIterator.hasNext()) 
          {
             Cell cell = cellIterator.next();
             switch (cell.getCellType()) 
             {
                //case Cell.CELL_TYPE_NUMERIC:
                //System.out.print( 
                //cell.getNumericCellValue() + " \t\t " );
                //break;
                case Cell.CELL_TYPE_NUMERIC:
                    break;
                case Cell.CELL_TYPE_STRING: 
                    switch (cell.getStringCellValue())
                    {
                       case "inf":
                           cell.setCellValue(100000000);
                       default:
                           break;
                    }
                //cell.setCellValue(1000000000);
                //System.out.print(
                //cell.getStringCellValue() + " \t\t " );
                break;
                default:
                    break;
             }
          }
          System.out.println("aaa");
          //workbook.close();
       }
       fis.close();
       FileOutputStream out = new FileOutputStream( new File(DataPreprocess.class.getResource("/").getPath()+"fileoutput.xlsx"));
       workbook.write(out);
       out.flush();
       out.close();
       workbook.close();
    }


}

这个例子中,输入文件中大部分是数字类型的,有一些字符类型的,这个例子吧string类型的字符串换成了数字。
以上就是使用Apache POI处理EXCEL数据的例子。

你可能感兴趣的:(WEKA)