生活随笔
收集整理的這篇文章主要介紹了
有关XLS文件的读取
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java程序有關XLS表格文件的讀取
IO流
.
在我看來,IO流核心便是讀文件和寫文件操作,有同學委托幫忙,今天嘗試寫了有關xls文件的讀取
代碼如下
package com.dehong.test;import java.io.*;import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;class XlsHelper {//得到導入數據至數據庫public void getDataFromExcel(String filePath,int index,int sheetHead) {if(!filePath.endsWith(".xls")) {System.out.println("格式不支持");}//定義FileInputStream fis=null;Workbook workbook=null;//初始化try {fis=new FileInputStream(filePath);workbook = new HSSFWorkbook(fis);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}//得到第幾張表Sheet sheet=workbook.getSheetAt(index);//得到表頭Row rowHead=sheet.getRow(sheetHead);//獲取表格長度int totalColNum=rowHead.getLastCellNum();int totalRowNum=sheet.getLastRowNum();//設置表頭數組String []heads=new String[totalColNum];//設置數據數組String []parameters=new String[totalColNum];//得到表頭列表for(int i=0;i<totalColNum-1;i++) {//cell.setCellType(CellType.STRING);Cell cell=rowHead.getCell((short)i);cell.setCellType(CellType.STRING);heads[i]=cell.getStringCellValue().trim();}//獲得所有數據for(int i = sheetHead+1 ; i <= totalRowNum ; i++){//獲得第i行對象Row row = sheet.getRow(i);//遍歷這一行所有數據for(int j=0;j<totalColNum;j++) {Cell cell=row.getCell(j);cell.setCellType(CellType.STRING);parameters [j]=cell.getStringCellValue().trim();System.out.println(parameters [j]);}}}
}
說說寫的過程遇到的問題:列的個數獲取總是出錯,現在的代碼依然有點小問題,getLastColNum();方法getPhysicalNumberOfCells();方法都不是很好用,我也沒找到更好的方法,只能湊合用了,由于項目需要導入數據到數據庫中,目前只能做到取出數據,明天繼續更新,嘗試解決xls表和數據庫不匹配的問題。
同時我還寫了一個不錯的SqlHelper類,分享并記錄下來,便于以后觀看
package com.dehong.test;import java.sql.*;/*** 數據庫工具* 查詢操作* 更新操作* 日期:2019年3月8日* @author HecW**/
class SqlHelper {//定義驅動和連接private static String url="jdbc:sqlserver://localhost:1433;DatabaseName=#";private static String username="sa";private static String password="cup2016";private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//定義要使用的類private static PreparedStatement ps=null;private static Connection ct=null;private static ResultSet rs=null;//初始化連接static {try {Class.forName(driver);System.out.println("數據庫加載成功");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}//得到連接public static Connection getConnection() {try {ct=DriverManager.getConnection(url, username, password);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("數據庫連接成功");return ct;}//查詢數據public static ResultSet query(String sql,String[] parameters) {try {ct=getConnection();if(parameters!=null) {ps=ct.prepareStatement(sql);for(int i=0;i<parameters.length;i++) {ps.setString(i+1, parameters[i]);}}rs=ps.executeQuery();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {//暫時不能關閉資源,結果未處理}return rs;}//更新操作方法//注意要實現多條sql語句的更新操作除了雙重for循環以外,還要將連接ct.setAutoCommit(flase);設為false,如果出現異常則rollback,正常則ct.commit();public static void update(String sql,String []parameters) {try {ct=getConnection();ps=ct.prepareStatement(sql);if(parameters!=null) {for(int i=0;i<parameters.length;i++) {ps.setString(i+1, parameters[i]);}}ps.executeUpdate();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally {close(ps,rs,ct);}}//關閉資源的函數public static void close(PreparedStatement ps,ResultSet rs,Connection ct) {if(rs!=null) {try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}rs=null;//加快垃圾回收}if(ps!=null) {try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}ps=null;//加快垃圾回收}if(ct!=null) {try {ct.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}ct=null;//加快垃圾回收}}//提供私有屬性的get方法public static String getUrl() {return url;}public static String getUsername() {return username;}public static String getPassword() {return password;}public static String getDriver() {return driver;}public static PreparedStatement getPs() {return ps;}public static Connection getCt() {return ct;}public static ResultSet getRs() {return rs;}}
總結
以上是生活随笔為你收集整理的有关XLS文件的读取的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。