【POI】修改已存在的xls,新添一列后,再保存本文件+獲取最大有效行號+獲取單元格內容


使用POI版本:

 

③ ④

 

 

 

 

  1 package com.poi.dealXlsx;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 
 11 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 12 import org.apache.poi.ss.usermodel.Cell;
 13 import org.apache.poi.ss.usermodel.Row;
 14 import org.apache.poi.ss.usermodel.Sheet;
 15 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 16 import org.junit.Test;
 17 
 18 public class DealXlsx {
 19     
 20     /**
 21      * ①有一組xlsx文件,每個xlsx中有一個工作簿。
 22      * ②另外有一個xlsx中的某一個標准列
 23      * ③從②中將標准列插入到①中的每一個文件的工作薄中
 24      * ④對比①中第46列的數據和插入的這一列,看看每一行出現在新插入這一列的哪一行
 25      * @throws IOException
 26      * @throws InvalidFormatException
 27      */
 28     @Test
 29     public void dealXlsxS() throws IOException, InvalidFormatException{
 30         insertOneLine();
 31         readAllFile();
 32     }
 33     
 34     /**
 35      * 將包含的點插入到每一個xlsx中
 36      * @throws InvalidFormatException
 37      * @throws IOException
 38      */
 39     public void insertOneLine() throws InvalidFormatException, IOException{
 40         
 41         List<String>  mutationIds = getList();
 42         File file1 = new File("D:/基因數據測試");
 43         File [] files = file1.listFiles();
 44 
 45         for (int i = 0; i < files.length; i++) {
 46             FileInputStream fileInputStream = new FileInputStream(files[i]);
 47             
 48             XSSFWorkbook workbook1 = new XSSFWorkbook(fileInputStream);
 49             Sheet sheet1 = workbook1.getSheetAt(0);
 50             
 51             if(sheet1 != null){
 52                 Row row = null;
 53                 int lastRowNum = sheet1.getLastRowNum();
 54                 for (int i1 = 0; i1 < 4241; i1++) {
 55                     if(i1 > lastRowNum ){
 56                         row = sheet1.createRow(i1);
 57                     }else{
 58                         row = sheet1.getRow(i1);
 59                     }
 60                     if(row != null){
 61                         Cell cell = row.createCell(64);
 62                         cell.setCellValue(mutationIds.get(i1));
 63                     }
 64                 }
 65             }
 66             System.out.println("插入第"+(i+1)+"個文件");
 67             FileOutputStream outPutStream = new FileOutputStream(files[i]);
 68             workbook1.write(outPutStream);
 69             outPutStream.close();
 70             workbook1.close();
 71         }
 72     }
 73     
 74     
 75     /**
 76      * 執行對比操作
 77      * @throws IOException
 78      * @throws InvalidFormatException 
 79      */
 80     public void readAllFile() throws IOException, InvalidFormatException{
 81         File file = new File("D:/基因數據測試");
 82         File [] files = file.listFiles();
 83         //獲取插入的標准列
 84         List<String>  mutationIds = getList();
 85         for (File file2 : files) {
 86             FileInputStream fileInputStream = new FileInputStream(file2);
 87             
 88             XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
 89             Sheet sheet = workbook.getSheetAt(0);
 90             if(sheet != null){
 91                 Row row = null;
 92                 //循環每一行
 93                 int maxRowNum = just4MaxRowNum(sheet);
 94                 for (int i = 0; i < maxRowNum; i++) {
 95                     row = sheet.getRow(i);
 96                     //如果是第一行 跳過
 97                     if(row.getRowNum() == 0){
 98                         Cell cell = row.createCell(65);
 99                         cell.setCellValue("目標行數");
100                         continue;
101                     }
102                     //獲取到46列的對比列 單元格中的數據
103                     Cell cell = row.getCell(46);
104                     String cellValue = getCellValue(cell);
105                     //對數據進行截取
106                     cellValue = cellValue.substring(cellValue.indexOf("=")+1, cellValue.indexOf(";"));
107                     //將數據拆分或者放入數組中
108                     String [] cVs = null;
109                     if(cellValue.contains(",")){
110                         cVs = cellValue.split(",");
111                     }else{
112                         cVs = new String[]{cellValue};
113                     }
114                     //對比List集合中的什么位置,返回位置,如果沒有,返回-1
115                     int thisNum = -1;
116                     for (int i1 = 0; i1 < cVs.length; i1++) {
117                         thisNum =  mutationIds.indexOf(cVs[i1]);
118                         if(thisNum > -1){
119                             break;
120                         }
121                     }
122                     
123                     //如果存在值,將位置值寫入最后一列
124                     if(thisNum > -1){
125                         cell = row.createCell(65);
126                         cell.setCellValue(String.valueOf(thisNum+1));
127                     }
128                     
129                 }
130                 
131             }
132             FileOutputStream outPutStream = new FileOutputStream(file2);
133             workbook.write(outPutStream);
134             outPutStream.close();
135             workbook.close();
136             
137         }
138     }
139     
140     /**
141      * 獲取當前單元格內容
142      * @param cell
143      * @return
144      */
145     public String getCellValue(Cell cell){
146         String cellVaule = null;
147         switch (cell.getCellType()) {
148         case 0: cellVaule = String.valueOf(cell.getNumericCellValue());break;
149         case 1: cellVaule = cell.getStringCellValue();break;
150         case 2: cellVaule = cell.getStringCellValue();break;
151         case 3: cellVaule = null;break;
152         case 4: cellVaule = String.valueOf(cell.getBooleanCellValue());break;
153         case 5: cellVaule = String.valueOf(cell.getErrorCellValue());break;
154 
155         default:cellVaule = null; break;
156         }
157         
158         return cellVaule.trim();
159     }
160     
161     
162     /**
163      * 獲取到包含的點 的數據 用於插入每一個xlsx中
164      * @return
165      * @throws InvalidFormatException
166      * @throws IOException
167      */
168     public List<String> getList() throws InvalidFormatException, IOException{
169         File file = new File("D:/基因數據2/時代基因175精簡版探針20170629定稿.xlsx");
170         XSSFWorkbook workbook = new XSSFWorkbook(file);
171         List<String>  mutationIds = new ArrayList<String>();
172         Sheet sheet = workbook.getSheet("包含的點");
173         if(sheet != null){
174             Row row = null;
175             for (int i = 0; i < 4241; i++) {
176                 row = sheet.getRow(i);
177                 if(row != null){
178                     mutationIds.add(getCellValue(row.getCell(4)));
179                 }
180             }
181             
182             System.out.println("包含的點總共有:"+mutationIds.size());
183         }
184         
185         return mutationIds;
186     }
187     
188     
189     /**
190      * 獲取最大行數  由於人為原因  xls中某個單元格中內容雖然已經刪除 但是單元格的對象依舊創建,因此需要自己獲取有效行數
191      * @param sheet
192      * @return
193      */
194     public int just4MaxRowNum(Sheet sheet){
195         int maxRowNum = sheet.getLastRowNum();//獲取最大行號  但不是有效行號
196         
197         for (int i = 5; i < maxRowNum; i++) {
198             Row row = sheet.getRow(i);
199             Cell cell = row.getCell(46);
200             if(cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK){//判斷cell單元格為null或者單元格類型為blank就表示此單元格沒有數據  那這一行的上一行就是有效行數
201                 maxRowNum = i-1;
202                 break;
203             }
204         }    
205         return maxRowNum;
206     }
207     
208 }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM