• Java解析excel


    <dependencies>
          <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-scratchpad</artifactId>
                <version>3.14</version>
            </dependency>
      </dependencies>
    package excel;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class TT {
        public static void main(String[] args) {
            try {
                File file=new File("src/main/java/excel/1.xls");
                String name=file.getName();
                boolean is2007=true;
                if(name.matches("^.+\.(?i)(xls)$")){
                    is2007=false;
                }else if (name.matches("^.+\.(?i)(xlsx)$")) {
                    is2007=true;
                }else{
                    throw new Exception("未知的版本");
                }
                InputStream is=new FileInputStream(file);
                Workbook wb=null;
                if(is2007){
                    wb=new XSSFWorkbook(is);
                }else{
                    wb=new HSSFWorkbook(is);
                }
                Sheet sheet=wb.getSheetAt(0);
                //得到行数
                int totalRows=sheet.getPhysicalNumberOfRows();
                List<String> list=new ArrayList<String>();
                //得到列数
                int totalCells=sheet.getRow(0).getPhysicalNumberOfCells();
                for(int i=0;i<totalRows;i++){
                    Row row=sheet.getRow(i);
                    for(int j=0;j<totalCells;j++){
                        Cell cell=row.getCell(j);
                        if(i==0){
                            list.add(cell.getStringCellValue());
                        }else{
                            break;
                        }
                    }
                    break;
                }
                
                
                for(int i=0;i<totalRows;i++){
                    Row row=sheet.getRow(i);
                    System.out.println("============"+(i+1));
                    for(int j=0;j<totalCells;j++){
                        Cell cell=row.getCell(j);
                        String v="";
                        if(cell==null){
                            v="";
                        }else{
                           int t=cell.getCellType();
                            if(XSSFCell.CELL_TYPE_NUMERIC==t){
                                double d=cell.getNumericCellValue();
                                String value=Double.toString(d);
                                int len=value.length();
                                int index=value.indexOf(".");
                                int pointW=4;//保留小数位数
                                pointW++;
                                if(index>0&&(index+pointW)<len){
                                    v=value.substring(0,index+pointW);
                                }else{
                                    v=value;
                                }
                            }else{
                                cell.setCellType(Cell.CELL_TYPE_STRING);
                                v=cell.getStringCellValue();
                            } } System.out.print(list.get(j)
    +":"+v+"|"); } System.out.println(); } } catch (Exception e) { e.printStackTrace(); } } }
  • 相关阅读:
    hdoj 2544 最短路径
    树状数组 hdoj1166
    并查集学习
    1402大数相乘 FFT算法学习!
    hdu1014
    动态规划 简单题dp
    迷宫路径
    简单的动态规划dp
    poj 3282 (bFS)
    背包问题,看不懂,啊!!!!!!!!dp
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/8578956.html
Copyright © 2020-2023  润新知