java读取excel格式的内容(APACHE POI)
来源:互联网 发布:淘宝卖家看到买家信息 编辑:程序博客网 时间:2024/05/22 09:50
首先导入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 0
- java读取excel格式的内容(APACHE POI)
- java读取word格式.doc或者.docx中的内容(APACHE POI)
- 【JAVA】apache poi excel 文件读取,各种数据类型,不规则excel格式都可以读取
- 【JAVA】apache poi excel 文件读取,各种数据类型,不规则excel格式都可以读取
- JAVA读取Excel任何格式的内容
- [JAVA] java读取Excel文件,Apache POI
- java使用poi读取xlsx格式的Excel总结
- 【java】使用POI读取excel文件内容
- java poi读取excel文件内容
- JAVA利用Apache Poi读取Excel文件
- 【Apache POI】Java 读取Excel文件
- java poi读取excel日期格式数据
- java poi读取excel日期格式数据
- poi读取excel内容
- poi读取Excel内容
- POI读取加密的EXCEL(兼容XLS,XLSX格式)
- Apache POI 之 初学实战篇 (六) --- 读取Excel内容
- (5)POI读取Excel内容
- iOS10通知框架UserNotification理解与应用 一、引言 关于通知,无论与远程Push还是本地通知,以往的iOS系统暴漏给开发者的接口都是十分有限的,开发者只能对标题和
- C语言基础——函数
- 利用adb对android手机进行远程截图/截屏
- Eclipse使用问题(一)
- 常见的4条用户体验误区
- java读取excel格式的内容(APACHE POI)
- spring读取properties文件
- 矩阵快速幂的复习。。。
- c++异常处理格式
- ReactNative,第一次运行的时候报错InstallException
- POJ 2348 Euclid's Game
- 关于动态权限申请的一点总结
- 【HDU
- CodeForces