使用POI版本:

③ ④

 package com.poi.dealXlsx;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test; public class DealXlsx { /**
* ①有一组xlsx文件,每个xlsx中有一个工作簿。
* ②另外有一个xlsx中的某一个标准列
* ③从②中将标准列插入到①中的每一个文件的工作薄中
* ④对比①中第46列的数据和插入的这一列,看看每一行出现在新插入这一列的哪一行
* @throws IOException
* @throws InvalidFormatException
*/
@Test
public void dealXlsxS() throws IOException, InvalidFormatException{
insertOneLine();
readAllFile();
} /**
* 将包含的点插入到每一个xlsx中
* @throws InvalidFormatException
* @throws IOException
*/
public void insertOneLine() throws InvalidFormatException, IOException{ List<String> mutationIds = getList();
File file1 = new File("D:/基因数据测试");
File [] files = file1.listFiles(); for (int i = 0; i < files.length; i++) {
FileInputStream fileInputStream = new FileInputStream(files[i]); XSSFWorkbook workbook1 = new XSSFWorkbook(fileInputStream);
Sheet sheet1 = workbook1.getSheetAt(0); if(sheet1 != null){
Row row = null;
int lastRowNum = sheet1.getLastRowNum();
for (int i1 = 0; i1 < 4241; i1++) {
if(i1 > lastRowNum ){
row = sheet1.createRow(i1);
}else{
row = sheet1.getRow(i1);
}
if(row != null){
Cell cell = row.createCell(64);
cell.setCellValue(mutationIds.get(i1));
}
}
}
System.out.println("插入第"+(i+1)+"个文件");
FileOutputStream outPutStream = new FileOutputStream(files[i]);
workbook1.write(outPutStream);
outPutStream.close();
workbook1.close();
}
} /**
* 执行对比操作
* @throws IOException
* @throws InvalidFormatException
*/
public void readAllFile() throws IOException, InvalidFormatException{
File file = new File("D:/基因数据测试");
File [] files = file.listFiles();
//获取插入的标准列
List<String> mutationIds = getList();
for (File file2 : files) {
FileInputStream fileInputStream = new FileInputStream(file2); XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
if(sheet != null){
Row row = null;
//循环每一行
int maxRowNum = just4MaxRowNum(sheet);
for (int i = 0; i < maxRowNum; i++) {
row = sheet.getRow(i);
//如果是第一行 跳过
if(row.getRowNum() == 0){
Cell cell = row.createCell(65);
cell.setCellValue("目标行数");
continue;
}
//获取到46列的对比列 单元格中的数据
Cell cell = row.getCell(46);
String cellValue = getCellValue(cell);
//对数据进行截取
cellValue = cellValue.substring(cellValue.indexOf("=")+1, cellValue.indexOf(";"));
//将数据拆分或者放入数组中
String [] cVs = null;
if(cellValue.contains(",")){
cVs = cellValue.split(",");
}else{
cVs = new String[]{cellValue};
}
//对比List集合中的什么位置,返回位置,如果没有,返回-1
int thisNum = -1;
for (int i1 = 0; i1 < cVs.length; i1++) {
thisNum = mutationIds.indexOf(cVs[i1]);
if(thisNum > -1){
break;
}
} //如果存在值,将位置值写入最后一列
if(thisNum > -1){
cell = row.createCell(65);
cell.setCellValue(String.valueOf(thisNum+1));
} } }
FileOutputStream outPutStream = new FileOutputStream(file2);
workbook.write(outPutStream);
outPutStream.close();
workbook.close(); }
} /**
* 获取当前单元格内容
* @param cell
* @return
*/
public String getCellValue(Cell cell){
String cellVaule = null;
switch (cell.getCellType()) {
case 0: cellVaule = String.valueOf(cell.getNumericCellValue());break;
case 1: cellVaule = cell.getStringCellValue();break;
case 2: cellVaule = cell.getStringCellValue();break;
case 3: cellVaule = null;break;
case 4: cellVaule = String.valueOf(cell.getBooleanCellValue());break;
case 5: cellVaule = String.valueOf(cell.getErrorCellValue());break; default:cellVaule = null; break;
} return cellVaule.trim();
} /**
* 获取到包含的点 的数据 用于插入每一个xlsx中
* @return
* @throws InvalidFormatException
* @throws IOException
*/
public List<String> getList() throws InvalidFormatException, IOException{
File file = new File("D:/基因数据2/时代基因175精简版探针20170629定稿.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(file);
List<String> mutationIds = new ArrayList<String>();
Sheet sheet = workbook.getSheet("包含的点");
if(sheet != null){
Row row = null;
for (int i = 0; i < 4241; i++) {
row = sheet.getRow(i);
if(row != null){
mutationIds.add(getCellValue(row.getCell(4)));
}
} System.out.println("包含的点总共有:"+mutationIds.size());
} return mutationIds;
} /**
* 获取最大行数 由于人为原因 xls中某个单元格中内容虽然已经删除 但是单元格的对象依旧创建,因此需要自己获取有效行数
* @param sheet
* @return
*/
public int just4MaxRowNum(Sheet sheet){
int maxRowNum = sheet.getLastRowNum();//获取最大行号 但不是有效行号 for (int i = 5; i < maxRowNum; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(46);
if(cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK){//判断cell单元格为null或者单元格类型为blank就表示此单元格没有数据 那这一行的上一行就是有效行数
maxRowNum = i-1;
break;
}
}
return maxRowNum;
} }

【POI】修改已存在的xls,新添一列后,再保存本文件+获取最大有效行号+获取单元格内容的更多相关文章

  1. POI教程之第二讲:创建一个时间格式的单元格,处理不同内容格式的单元格,遍历工作簿的行和列并获取单元格内容,文本提取

    第二讲 1.创建一个时间格式的单元格 Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet=wb.createSheet("第一个 ...

  2. poi 升级至4.x 的问题总结(POI Excel 单元格内容类型判断并取值)

    POI Excel 单元格内容类型判断并取值 以前用 cell.getCachedFormulaResultType() 得到 type 升级到4后获取不到了 换为:cell.getCellType( ...

  3. 使用poi导出Excel,并设定单元格内容类型,抛出异常

    本例子使用的是HSSF,为Excel2003提供处理方案. 设定为输入类型为数值 import org.apache.poi.hssf.usermodel.DVConstraint; import o ...

  4. 1 npoi 网上 不用模板 设置密码 workbook.WriteProtectWorkbook("password", "admin"); 、、 2 locked.IsLocked = true; sheet1.ProtectSheet("password");NPOI操作EXCEL--设置密码才可以修改单元格内容 3 模板设置密码 确定原密码 设置新密码

    1 workbook.WriteProtectWorkbook("password", "admin"); 还是可以进去 只读进去 可以编辑 编辑就另存为   ...

  5. 2.6.2 用NPOI操作EXCEL--设置密码才可以修改单元格内容

    2.6.2 用NPOI操作EXCEL--设置密码       有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完 ...

  6. WinForm------GridControl单元格内容修改外表样式

    private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDra ...

  7. POI Excel 单元格内容类型判断并取值

    个人用到的 String birthdayVal = null;                                                                     ...

  8. poi 取excel单元格内容时,需要判断单元格的类型,才能正确取出

    以下内容非原创,原文链接http://blog.sina.com.cn/s/blog_4b5bc01101015iuq.html ate String getCellValue(HSSFCell ce ...

  9. 关于poi导出excel方式HSSFWorkbook(xls).XSSFWorkbook(xlsx).SXSSFWorkbook.csv的总结

    1.HSSFWorkbook(xls) import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermo ...

随机推荐

  1. Spring 对象的声明与注入

    1.怎么把一个对象该过过交给Spring管理? 1)通过xml中配置<bean>节点来声明 2)通过Spring注解来声明,如:@Service.@Repository.@Componen ...

  2. Nios II 中的缓存和内存数据的读写

    nios 使用地址中31bit来表示访问是否bypass cache.如果bit 31=0 表示不bypass cache,即使用cache里的数据:如果bit 31=1表示bypass cache, ...

  3. HDU5772 String problem

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  4. 安装l Xposed Framework

    How to install Xposed Framework on Android 4.x.x :   1. For Android 4.0.3 through 4.4.4 Visit this X ...

  5. 内置函数补充,__str__方法、__del__方法 和 __call__方法和元祖

    一 .内置函数补充 1.isinstance函数: isinstance(obj,cls)检查obj是否是类 cls 的对象 使用该函数来判断一个函数的类型 2. issubclass(sub, su ...

  6. linux基础-临时和永久修改ip地址以及通配符相关

    一.临时配置网络(ip,网关,dns) 修改临时ip地址: 1.ifconfig查看当前的网卡和ip地址 2.临时修改IP地址:ifconfig ens32 192.168.16.200/24,ifc ...

  7. Windows下搭建本地SVN服务器【转】

    转自:http://www.linuxidc.com/Linux/2015-01/111563.htm 本文介绍Windows下搭建本地SVN服务器的方法,网上资料比较少也比较旧,大都介绍的是旧版本S ...

  8. pool.map的第二个参数想传入多个咋整?

    from functools import partial from multiprocessing import Pool as ThreadPool pageurls=[] if maxpage: ...

  9. 安装VMware Tools的步骤和那些坑

    背景环境:VMware workstation 12.5+Ubuntu16.04 首先VMware Tools在ubuntu中是及其不稳定的,也就是说,当你点击菜单栏中的install vmware ...

  10. dependencyManagement和dependencies的区别

    参考:http://zhaoshijie.iteye.com/blog/2094478http://blog.csdn.net/cpf2016/article/details/45674377 还有一 ...