java读取excel格式的内容(APACHE POI)

2 篇文章 0 订阅

首先导入poi的jar包:
这里写图片描述
java代码:

`public class ImportToSql {
    public static void main(String[] args) throws Exception {  

        System.out.println("-------------");  
       // readXml("d:/test2.xls");
        String result = readXml("C:\\Users\\czp\\Desktop\\topic\\hebing1.xlsx");  
        write(result,"");
        System.out.println("写入完毕");
    }  
    public static String readXml(String fileName){  
        String resultStr="name,alternateName,catagrory,subCatagrory,description,END"+"\r\n";
        boolean isE2007 = false;    //判断是否是excel2007格式  
        if(fileName.endsWith("xlsx"))  
            isE2007 = true;  
        try {  
            InputStream input = new FileInputStream(fileName);  //建立输入流  
            Workbook wb  = null;  
            //根据文件格式(2003或者2007)来初始化  
            if(isE2007)  
                wb = new XSSFWorkbook(input);  
            else  
                wb = new HSSFWorkbook(input);  
            Sheet sheet = wb.getSheetAt(0);     //获得第一个表单  
            Iterator<Row> rows = sheet.rowIterator(); //获得第一个表单的迭代器  
            while (rows.hasNext()) {  
                Row row = rows.next();  //获得行数据  
                System.out.println("Row #" + row.getRowNum());  //获得行号从0开始  
                if(row.getRowNum()>0){
                    resultStr+="END"+ "\r\n";
                }
                Iterator<Cell> cells = row.cellIterator();    //获得第一行的迭代器  
                while (cells.hasNext()) {  
                    Cell cell = cells.next();  
                    System.out.println("Cell #" + cell.getColumnIndex()); 
                    switch (cell.getCellType()) {   //根据cell中的类型来输出数据  
                    case HSSFCell.CELL_TYPE_NUMERIC:  
                        resultStr+=""+",";
                        System.out.println(cell.getNumericCellValue());  
                        break;  
                    case HSSFCell.CELL_TYPE_STRING:
                        String str="";
                        str=cell.getStringCellValue();
                        str=str.replaceAll(",",",");    //把英文的逗号换成中文的
                        str=str.replaceAll("\"","\"\"");//把双引号换为两个双引号,输出时默认为一个 "\"","\"\""
                        str=str.replaceAll(System.getProperty("line.separator"),"##");//把换行符换为##
                        str=str.replaceAll("\r|\n|\t", "");
                        resultStr+=str+",";
                        System.out.println(str);  
                        break;  
                    case HSSFCell.CELL_TYPE_BOOLEAN:  
                        System.out.println(cell.getBooleanCellValue());  
                        break;  
                    case HSSFCell.CELL_TYPE_FORMULA:  
                        System.out.println(cell.getCellFormula());  
                        break;  
                    default:
                        resultStr+=""+",";
                        System.out.println("unsuported sell type");  
                    break;  
                    }  
                }  
            }  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }
        return resultStr+="END"+ "\r\n";
    } 
    private static void write(String result, String toFile) throws Exception {

                OutputStream out = null;
                try {
                    File f=new File("C:\\Users\\czp\\Desktop\\topic\\1.csv");
                    if(f.exists()){
                        f.delete();
                    }
                    out = new FileOutputStream(f);

                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                out.write(result.toString().getBytes("gbk"));
                out.flush();
                out.close();
    }
}`
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值