POI操作excel

来源:互联网 发布:淘宝女装黑色毛衣 编辑:程序博客网 时间:2024/06/06 19:08
<pre name="code" class="java">public class ExcelUtil {    public Workbook createXlsxWorkbook()    {        return new XSSFWorkbook();    }    public static Workbook getWorkbook(String file)    {        if(StringUtils.isEmpty(file))            return null;        try {            return WorkbookFactory.create(new FileInputStream(file));        } catch (IOException e) {            e.printStackTrace();        } catch (InvalidFormatException e) {            e.printStackTrace();        }        return null;    }    public static Sheet getSheet(Workbook workbook, String sheetName)    {        if(workbook==null)            return null;        Sheet sheet = workbook.getSheet(sheetName);        if(sheet==null)            sheet=workbook.createSheet(sheetName);        return sheet;    }    public static Cell getCell(Sheet sheet,int row,int col)    {        if(sheet==null)            return null;        Row sheetrow = sheet.getRow(row);        if(sheetrow==null)            sheetrow=sheet.createRow(row);        Cell cell = sheetrow.getCell(col);        if(cell==null)            cell=sheetrow.createCell(col);        return cell;    }    public static void setText(Cell cell,String text)    {        cell.setCellValue(text);    }    public static String getValue(Cell cell)    {        return  cell.getStringCellValue();    }}
private static void importExcel(){    Workbook workbook = ExcelUtil.getWorkbook("E:/digital2016-09-21 10-49-39.xls");    Sheet sheet = ExcelUtil.getSheet(workbook, "非标设备");    for(Row row:sheet)    {        for(Cell cell:row)        {            if(cell!=null)            {                System.out.println(ExcelUtil.getValue(cell));            }        }    }}private static void export() {    XSSFWorkbook workbook = new XSSFWorkbook();//创建工作薄    //Workbook workbook = ExcelUtil.CreateWorkbook("F:/1.xlsx");    if(workbook!=null)    {        Sheet sheet = ExcelUtil.getSheet(workbook, "国标设备");        ExcelUtil.setText(ExcelUtil.getCell(sheet,0,0),"PVM");        ExcelUtil.setText(ExcelUtil.getCell(sheet,0,1),"10.11.68.1");        ExcelUtil.setText(ExcelUtil.getCell(sheet,1,0),"PVM");        ExcelUtil.setText(ExcelUtil.getCell(sheet,1,1),"10.11.68.1");        ExcelUtil.setText(ExcelUtil.getCell(sheet,2,0),"PVM");        FileOutputStream fileOut=null;        try {            fileOut = new FileOutputStream("E:/1.xlsx");            workbook.write(fileOut);        } catch (IOException e) {            e.printStackTrace();            if(fileOut!=null) {                try {                    fileOut.close();                } catch (IOException e1) {                    e1.printStackTrace();                }            }        }    }}

maven依赖
<pre name="code" class="html">   <!--exl表格文件解析-->            <dependency>                <groupId>org.apache.poi</groupId>                <artifactId>poi</artifactId>                <version>3.9</version>            </dependency>            <dependency>                <groupId>org.apache.poi</groupId>                <artifactId>poi-ooxml</artifactId>                <version>3.9</version>            </dependency>            <dependency>                <groupId>org.apache.poi</groupId>                <artifactId>poi-ooxml-schemas</artifactId>                <version>3.9</version>            </dependency>


                                             
0 0