1、Excel相關操作代碼
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.lang.reflect.Field; 7 import java.lang.reflect.Method; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import org.apache.poi.hssf.usermodel.HSSFRow; 12 import org.apache.poi.hssf.usermodel.HSSFSheet; 13 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 14 import org.apache.poi.ss.usermodel.Cell; 15 import org.apache.poi.ss.usermodel.Row; 16 import org.springframework.stereotype.Component; 17 18 /** 19 * @Description: 20 * @author 21 * @date 創建時間:2016年12月8日下午2:38:47 22 * @version 1.0 23 */ 24 @Component 25 public class ExcelManage { 26 private HSSFWorkbook workbook = null; 27 28 /** 29 * 判斷文件是否存在 30 * @param filePath 文件路徑 31 * @return 32 */ 33 public boolean fileExist(String filePath){ 34 boolean flag = false; 35 File file = new File(filePath); 36 flag = file.exists(); 37 return flag; 38 } 39 40 /** 41 * 判斷文件的sheet是否存在 42 * @param filePath 文件路徑 43 * @param sheetName 表格索引名 44 * @return 45 */ 46 public boolean sheetExist(String filePath,String sheetName){ 47 boolean flag = false; 48 File file = new File(filePath); 49 if(file.exists()){ //文件存在 50 //創建workbook 51 try { 52 workbook = new HSSFWorkbook(new FileInputStream(file)); 53 //添加Worksheet(不添加sheet時生成的xls文件打開時會報錯) 54 HSSFSheet sheet = workbook.getSheet(sheetName); 55 if(sheet!=null) 56 flag = true; 57 } catch (Exception e) { 58 e.printStackTrace(); 59 } 60 }else{ //文件不存在 61 flag = false; 62 } 63 return flag; 64 } 65 /** 66 * 創建新Sheet並寫入第一行數據 67 * @param filePath excel的路徑 68 * @param sheetName 要創建的表格索引 69 * @param titleRow excel的第一行即表格頭 70 * @throws IOException 71 * @throws FileNotFoundException 72 */ 73 public void createSheet(String filePath,String sheetName,String titleRow[]) throws FileNotFoundException, IOException{ 74 FileOutputStream out = null; 75 File excel = new File(filePath); // 讀取文件 76 FileInputStream in = new FileInputStream(excel); // 轉換為流 77 workbook = new HSSFWorkbook(in); // 加載excel的 工作目錄 78 79 workbook.createSheet(sheetName); // 添加一個新的sheet 80 //添加表頭 81 Row row = workbook.getSheet(sheetName).createRow(0); //創建第一行 82 try { 83 for(int i = 0;i < titleRow.length;i++){ 84 Cell cell = row.createCell(i); 85 cell.setCellValue(titleRow[i]); 86 } 87 out = new FileOutputStream(filePath); 88 workbook.write(out); 89 }catch (Exception e) { 90 e.printStackTrace(); 91 }finally { 92 try { 93 out.close(); 94 } catch (IOException e) { 95 e.printStackTrace(); 96 } 97 } 98 } 99 /** 100 * 創建新excel. 101 * @param filePath excel的路徑 102 * @param sheetName 要創建的表格索引 103 * @param titleRow excel的第一行即表格頭 104 */ 105 public void createExcel(String filePath,String sheetName,String titleRow[]){ 106 //創建workbook 107 workbook = new HSSFWorkbook(); 108 //添加Worksheet(不添加sheet時生成的xls文件打開時會報錯) 109 workbook.createSheet(sheetName); 110 //新建文件 111 FileOutputStream out = null; 112 try { 113 //添加表頭 114 Row row = workbook.getSheet(sheetName).createRow(0); //創建第一行 115 for(int i = 0;i < titleRow.length;i++){ 116 Cell cell = row.createCell(i); 117 cell.setCellValue(titleRow[i]); 118 } 119 out = new FileOutputStream(filePath); 120 workbook.write(out); 121 } catch (Exception e) { 122 e.printStackTrace(); 123 } finally { 124 try { 125 out.close(); 126 } catch (IOException e) { 127 e.printStackTrace(); 128 } 129 } 130 } 131 /** 132 * 刪除文件. 133 * @param filePath 文件路徑 134 */ 135 public boolean deleteExcel(String filePath){ 136 boolean flag = false; 137 File file = new File(filePath); 138 // 判斷目錄或文件是否存在 139 if (!file.exists()) { 140 return flag; 141 } else { 142 // 判斷是否為文件 143 if (file.isFile()) { // 為文件時調用刪除文件方法 144 file.delete(); 145 flag = true; 146 } 147 } 148 return flag; 149 } 150 /** 151 * 往excel中寫入. 152 * @param filePath 文件路徑 153 * @param sheetName 表格索引 154 * @param object 155 */ 156 public void writeToExcel(String filePath,String sheetName, Object object,String titleRow[]){ 157 //創建workbook 158 File file = new File(filePath); 159 try { 160 workbook = new HSSFWorkbook(new FileInputStream(file)); 161 } catch (FileNotFoundException e) { 162 e.printStackTrace(); 163 } catch (IOException e) { 164 e.printStackTrace(); 165 } 166 FileOutputStream out = null; 167 HSSFSheet sheet = workbook.getSheet(sheetName); 168 // 獲取表格的總行數 169 int rowCount = sheet.getLastRowNum() + 1; // 需要加一 170 try { 171 Row row = sheet.createRow(rowCount); //最新要添加的一行 172 //通過反射獲得object的字段,對應表頭插入 173 // 獲取該對象的class對象 174 Class<? extends Object> class_ = object.getClass(); 175 176 for(int i = 0;i < titleRow.length;i++){ 177 String title = titleRow[i]; 178 String UTitle = Character.toUpperCase(title.charAt(0))+ title.substring(1, title.length()); // 使其首字母大寫; 179 String methodName = "get"+UTitle; 180 Method method = class_.getDeclaredMethod(methodName); // 設置要執行的方法 181 String data = method.invoke(object).toString(); // 執行該get方法,即要插入的數據 182 Cell cell = row.createCell(i); 183 cell.setCellValue(data); 184 } 185 out = new FileOutputStream(filePath); 186 workbook.write(out); 187 } catch (Exception e) { 188 e.printStackTrace(); 189 } finally { 190 try { 191 out.close(); 192 } catch (IOException e) { 193 e.printStackTrace(); 194 } 195 } 196 } 197 }
2、main方法調用方式
1 public static void main(String[] args) { 2 String filePath = "result/數據匯總.xls"; 3 String sheetName = "測試"; 4 //Excel文件易車sheet頁的第一行 5 String title[] = {"日期", "城市","新發布車源數"}; 6 //Excel文件易車每一列對應的數據 7 String titleDate[] = {"date", "city","newPublish"}; 8 9 ExcelManage em = new ExcelManage(); 10 //判斷該名稱的文件是否存在 11 boolean fileFlag = em.fileExist(filePath); 12 if(!fileFlag){ 13 em.createExcel(filePath,sheetName,title); 14 } 15 //判斷該名稱的Sheet是否存在 16 boolean sheetFlag = em.sheetExist(filePath,sheetName); 17 //如果該名稱的Sheet不存在,則新建一個新的Sheet 18 if(!sheetFlag){ 19 try { 20 em.createSheet(filePath,sheetName,title); 21 } catch (FileNotFoundException e) { 22 e.printStackTrace(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 } 28 YiCheData user = new YiCheData(); 29 user.setDate("206-12-21"); 30 user.setCity("北京"); 31 user.setNewPublish("5"); 32 //寫入到excel 33 em.writeToExcel(filePath,sheetName,user,titleDate); 34 }
3、用於測試的bean類
1 public class YiCheData { 2 private String date; 3 private String city; 4 private String newPublish; 5 6 public String getDate() { 7 return date; 8 } 9 public void setDate(String date) { 10 this.date = date; 11 } 12 public String getCity() { 13 return city; 14 } 15 public void setCity(String city) { 16 this.city = city; 17 } 18 public String getNewPublish() { 19 eturn newPublish; 20 } 21 public void setNewPublish(String newPublish) { 22 this.newPublish = newPublish; 23 } 24 }
以上代碼請參考:http://www.open-open.com/lib/view/open1433238476150.html