package ut;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import javax.swing.JOptionPane;
import org.apache.poi.ss.usermodel.Row;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.PageOrientation;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**不支持Excel2007格式(也就是xlsx格式文件)*/
public class ExcelUtil {
public static ArrayList<String[]> readRows(String f,int sheetIndex,int startRow) {
Workbook wb = null;
try {
wb = Workbook.getWorkbook(new File(f));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheets()[sheetIndex];
int rows = sheet.getRows();
ArrayList<String[]> list=new ArrayList<String[]>();
for(int i = startRow ;i < rows ; i++){
Cell[] cells = sheet.getRow(i);
String[] row=new String[cells.length];
for(int j=0;j<cells.length;j++){
row[j]=cells[j].getContents();
}
list.add(row);
}
return list;
}
public static ArrayList<String[]> readRows(String f,int sheetIndex,int startRow,int endRow) {
Workbook wb = null;
try {
wb = Workbook.getWorkbook(new File(f));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheets()[sheetIndex];
int rows = sheet.getRows();
ArrayList<String[]> list=new ArrayList<String[]>();
for(int i = startRow ;i <=endRow ; i++){
Cell[] cells = sheet.getRow(i);
String[] row=new String[cells.length];
for(int j=0;j<cells.length;j++){
row[j]=cells[j].getContents();
}
list.add(row);
}
return list;
}
/**获取多个sheets的行数据.sheetIndex<0||>length则获取所有Sheets,>0则获取index及之前的*/
public static ArrayList<String[]> readRowsOfSheets(String f,int sheetIndex,int startRow) {
Workbook wb = null;
try {
wb = Workbook.getWorkbook(new File(f));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//get sheets
Sheet[] sheets=wb.getSheets();
int end=sheetIndex;
if(sheetIndex<0||sheetIndex>sheets.length)
end=sheets.length;
ArrayList<String[]> list=new ArrayList<String[]>();
for(int k=0;k<end+1;k++){
Sheet sheet = sheets[k];
int rows = sheet.getRows();
for(int i = startRow ;i < rows ; i++){
Cell[] cells = sheet.getRow(i);
String[] row=new String[cells.length];
for(int j=0;j<cells.length;j++){
row[j]=cells[j].getContents();
}
list.add(row);
}
}
return list;
}
/**获取某个目录下的,所有表格文件的,指定sheets的,所有行的数据.endSheetIndex<0||>length则获取所有Sheets,>0则获取index及之前的*/
public static ArrayList<String[]> readRowsOfSheetsOfFiles(String path,int endSheetIndex,int startRow) {
String[] farr=FileUtil.getFilesFullPath(path);
if(farr==null)//是个文件
farr=new String[]{path};
ArrayList<String[]> list=new ArrayList<String[]>();
for(String f:farr){
System.out.println(f);
Workbook wb = null;
try {
wb = Workbook.getWorkbook(new File(f));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//get sheets
Sheet[] sheets=wb.getSheets();
int end=endSheetIndex;
if(endSheetIndex<0||endSheetIndex>sheets.length)
end=sheets.length;
for(int k=0;k<end+1;k++){
Sheet sheet = sheets[k];
int rows = sheet.getRows();
for(int i = startRow ;i < rows ; i++){
Cell[] cells = sheet.getRow(i);
String[] row=new String[cells.length];
for(int j=0;j<cells.length;j++){
row[j]=cells[j].getContents();
}
list.add(row);
}
}
}
return list;
}
public static ArrayList<String[]> readColumns(String f,int sheetIndex) {
Workbook wb = null;
try {
wb = Workbook.getWorkbook(new File(f));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheets()[sheetIndex];
int cols = sheet.getColumns();
ArrayList<String[]> list=new ArrayList<String[]>();
for(int i = 0 ;i < cols ; i++){
Cell[] cells = sheet.getColumn(i);
String[] col=new String[cells.length];
for(int j=0;j<cells.length;j++){
col[j]=cells[j].getContents();
}
list.add(col);
}
return list;
}
/**读取每一行里的指定列元素,按列的先后顺序放入一个数组里*/
public static ArrayList<String[]> readColumns(String f,int[] indexs,int sheetIndex) {
Workbook wb = null;
try {
wb = Workbook.getWorkbook(new File(f));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheets()[sheetIndex];
int rows = sheet.getRows();
ArrayList<String[]> list=new ArrayList<String[]>();
for(int i = 0 ;i < rows ; i++){
Cell[] cells = sheet.getRow(i);
String[] col=new String[indexs.length];
for(int j=0;j<indexs.length;j++){
col[j]=cells[indexs[j]].getContents();
}
list.add(col);
}
return list;
}
public static String[] readRow(String f,int sheetIndex,int rowIndex) {
Workbook wb = null;
try {
wb = Workbook.getWorkbook(new File(f));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheets()[sheetIndex];
// int rows = sheet.getRows();
Cell[] cells = sheet.getRow(rowIndex);
String[] row=new String[cells.length];
for(int j=0;j<cells.length;j++){
row[j]=cells[j].getContents();
}
return row;
}
/**endRow=0则不限底*/
public static String[] readColumn(String f,int sheetIndex,int colIndex,int startRow,int endRow) {
Workbook wb = null;
try {
wb = Workbook.getWorkbook(new File(f));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheets()[sheetIndex];
Cell[] cells = sheet.getColumn(colIndex);
int endIndex=endRow==0?cells.length-1:endRow;
String[] data=new String[endIndex-startRow+1];
for(int j=0;j<data.length;j++){
data[j]=cells[j+startRow].getContents();
}
return data;
}
/**endRow=0则不限底*/
public static String[] readColumn(String f,String sheetName,int colIndex,int startRow,int endRow) {
Workbook wb = null;
try {
wb = Workbook.getWorkbook(new File(f));
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sheet sheet = wb.getSheet(sheetName);
Cell[] cells = sheet.getColumn(colIndex);
int endIndex=endRow==0?cells.length-1:endRow;
String[] data=new String[endIndex-startRow+1];
for(int j=0;j<data.length;j++){
data[j]=cells[j+startRow].getContents();
}
return data;
}
/**逐行写入(多行)*/
public static void writeRows(File f,ArrayList<String[]> list,int startRow,int sheetIndex){
WritableWorkbook book=null;
try {
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
WritableSheet sheet = book.getSheet(sheetIndex);
//逐条注入数据
for(int i=0;i<list.size();i++){
String[] arr=list.get(i);
for(int j=0;j<arr.length;j++){
sheet.addCell(new Label(j, i+startRow, arr[j]));
}
}
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (RowsExceededException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "行号超出");
} catch (WriteException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "写入出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**写入一列数据*/
public static void writeColumn(File f,String[] arr,int columnIndex,int startRow,int sheetIndex){
WritableWorkbook book=null;
try {
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
WritableSheet sheet = book.getSheet(sheetIndex);
//逐条注入数据
for(int i=0;i<arr.length;i++){
String text=arr[i];
sheet.addCell(new Label(columnIndex, i+startRow, text));
}
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (RowsExceededException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "行号超出");
} catch (WriteException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "写入出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**定点写入一格,c、r为列行索引*/
public static void writeCell(String f,int c,int r,String text,int sheetIndex){
WritableWorkbook book=null;
try {
Workbook wb = Workbook.getWorkbook(new File(f));
book = Workbook.createWorkbook(new File(f), wb);//副本修改模式
WritableSheet sheet = book.getSheet(sheetIndex);
sheet.addCell(new Label(c, r, text));
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (RowsExceededException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "行号超出");
} catch (WriteException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "写入出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**根据标题字母获取表格列的索引[目前极限为dz-129]
* @param title 列字母代号,如g
* */
public static Integer Colo(String title){
if(title.trim().length()==0)return null;
title=title.toLowerCase();//先统一换成小写
ArrayList<String[]> arrs=FileUtil.readFileArrByRow(new File("ExColCharNumMap.txt").getPath());
for(String[] arr:arrs){
if(arr[0].equals(title))
return Integer.parseInt(arr[1]);
}
return null;
}
/**根据标题字母集获取表格列的索引集[目前极限为dz-129]
* @param title 列字母代号,如g
* */
public static int[] Colos(String[] titles){
if(titles==null||titles.length==0)return null;
int[] ins=new int[titles.length];
for(int i=0;i<titles.length;i++){
ins[i]=Colo(titles[i]);
}
return ins;
}
/**对表格某列数据进行关键字过滤,符合项标注颜色,类型为包含;颜色填null则默认为黄色*/
public static void markRow(String path,int[] colIns,String[] keys,jxl.format.Colour colour){
WritableWorkbook book=null;
try {
File f=new File(path);
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
WritableSheet sheet = book.getSheet(0);
int max=sheet.getRows();
for(int i=0;i<max;i++){
if(colIns==null){
colIns=UT.getIntArr(sheet.getRow(i).length);//每一行的列数可能不同
}
ArrayList<Cell> items=getRowCells(sheet, i, colIns);
if(items.size()>0){//一般为表头表尾非数据行
String[] itemsArr=getStringArrOfRow(items);
int index=UT.oneContains(itemsArr, keys);
if(index>-1){
WritableCellFormat cf=new WritableCellFormat(items.get(index).getCellFormat());//原版样式,保持一致
cf.setBackground(colour==null?Colour.YELLOW:colour);
//标注整行
Cell[] row=sheet.getRow(i);
for(int j=0;j<row.length;j++){
sheet.addCell(new Label(j,i,row[j].getContents(),cf));
}
}
}
}
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (RowsExceededException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "行号超出");
} catch (WriteException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "fillData写入模板出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**标记某一个单元格*/
public static void markColumnCell(File f,int sheetIndex,int colIndex,int rowIndex,jxl.format.Colour colour){
WritableWorkbook book=null;
try {
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
WritableSheet sheet = book.getSheet(sheetIndex);
Cell cell=sheet.getCell(colIndex, rowIndex);
WritableCellFormat cf=new WritableCellFormat();//原版样式,保持一致
cf.setBackground(colour==null?Colour.RED:colour);
sheet.addCell(new Label(colIndex,rowIndex,cell.getContents(),cf));
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (RowsExceededException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "行号超出");
} catch (WriteException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "fillData写入模板出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**获取某列包含某关键字的行数据*/
public static ArrayList<String[]> getCellColumnContains(String f,int c,String keyword){
ArrayList<String[]> data=readRows(f, 0,0);
ArrayList<String[]> list=new ArrayList<String[]>();
for(String[] arr:data){
if(arr[c].contains(keyword)){
list.add(arr);
}
}
return list;
}
/**新建栏目*/
public static void initSheets(File f,ArrayList<String> names){
WritableWorkbook book=null;
try {
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
//逐条注入数据
for(int i=0;i<names.size();i++){
String name=names.get(i);
// int startIndex =book.getSheets().length;
book.createSheet(name, i);
}
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void mergeCells(String path,String sheetName,int startC,int startR,int endC,int endR){
WritableWorkbook book=null;
try {
File f=new File(path);
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
WritableSheet sheet = book.getSheet(sheetName);
sheet.mergeCells(startC, startR, endC, endR);
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**合并一个列中内容相同的单元格(连续的)*/
public static void mergeSameColumnCells(String path,String sheetName,int colIndex,int startRow,int endRow){
WritableWorkbook book=null;
try {
File f=new File(path);
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
WritableSheet sheet = book.getSheet(sheetName);
HashMap<String,Integer[]> hm=new HashMap<String,Integer[]>();
String curKey="";
for(int i=startRow;i<=endRow;i++){
Cell cell=sheet.getCell(colIndex,i);
String content=cell.getContents().trim();
if(content.equals(curKey)){
Integer[] arr=hm.get(content);
arr[1]=i;
}else{
curKey=content;
Integer[] arr=new Integer[2];
arr[0]=i;
arr[1]=i;
hm.put(content, arr);
}
}
for(String key : hm.keySet()){
Integer[] arr=hm.get(key);
if(arr[1]>arr[0]){
sheet.mergeCells(colIndex, arr[0], colIndex, arr[1]);
}
}
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**合并一个列中每N个单元格*/
public static void mergeColumnCellsEveryN(String path,String sheetName,int colIndex,int startRow,int endRow,int n){
WritableWorkbook book=null;
try {
File f=new File(path);
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
WritableSheet sheet = book.getSheet(sheetName);
HashMap<String,Integer[]> hm=new HashMap<String,Integer[]>();
while(startRow+n<=endRow){
int start=startRow;
int end=startRow+n-1;
sheet.mergeCells(colIndex, start, colIndex, end);
startRow=startRow+n;
if(startRow+n>endRow){
sheet.mergeCells(colIndex, startRow, colIndex, endRow);
break;
}
}
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void delCols(File f,int indexTitle,String[] titles){
WritableWorkbook book=null;
try {
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);
WritableSheet sheet = book.getSheet(0);
Cell[] rowTitle=sheet.getRow(indexTitle);
ArrayList<Integer> indexs=new ArrayList<Integer>();//放置索引
for(int i=0;i<rowTitle.length;i++){
String s=rowTitle[i].getContents().trim();
for(String title:titles){
if(s.equals(title)){
indexs.add(i);
break;
}
}
}
Collections.sort(indexs);
for(int i=indexs.size()-1;i>-1;i--){
sheet.removeColumn(indexs.get(i));
}
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**f如果是目录则获取目录下所有;sheetIndex<0||>length则获取所有Sheets,>0则获取index及之前的*/
public static HashMap<String,Double> getTotalValueFromSheets(String[] keys,String f,int colIndex,int sheetIndex){
ArrayList<String[]> rows=readRowsOfSheetsOfFiles(f, 0, 0);
//finditem
ArrayList<String[]> kvList=new ArrayList<String[]>();
for(String[] arr:rows){
for(String item:arr){//模糊匹配
int in=UT.containsOne(item, keys);
if(in>-1){
kvList.add(new String[]{keys[in],arr[colIndex]});
}
}
}
HashMap<String, Double> map=UT.addKVlist(kvList);
UT.printMapDouble(map);
return map;
}
/**根据关键字删除行*/
public static void delRows(String path,String[] keys,int colIndex,int sheetIndex){
WritableWorkbook book=null;
try {
File f=new File(path);
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
WritableSheet sheet = book.getSheet(sheetIndex);
//遍历每一行
for(int i=sheet.getRows()-1;i>-1;i--){
Cell[] cell=sheet.getRow(i);
if(colIndex<0||colIndex>cell.length-1){//行包含
if(cellsContains(cell, keys)){
sheet.removeRow(i);
}
}else{//某列包含
if(UT.containsOne(cell[colIndex].getContents(), keys)>-1){
sheet.removeRow(i);
}
}
}
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**根据序号删除行*/
public static void delRows(String path,ArrayList<Integer> ins,int sheetIndex){
WritableWorkbook book=null;
try {
File f=new File(path);
Workbook wb = Workbook.getWorkbook(f);
book = Workbook.createWorkbook(f, wb);//副本修改模式
WritableSheet sheet = book.getSheet(sheetIndex);
int startRow=getStartRow(sheet, sheet.getColumns());
//遍历每一行
for(int i=ins.size()-1;i>-1;i--){//表头不删
int index=ins.get(i);
if(index>startRow)
sheet.removeRow(index);
}
book.write();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "文件处理出错");
} catch (BiffException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "复制模板失败");
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void delRows(WritableSheet sheet,ArrayList<Integer> ins){
for(int i=ins.size()-1;i>-1;i--){
sheet.removeRow(ins.get(i));
}
}
static boolean cellsContains(Cell[] cell,String[] keys){
for(int j=0;j<cell.length;j++){
for(String key:keys){
if(cell[j].getContents().contains(key)){
return true;
}
}
}
return false;
}
/**从某个表获取指定项目(可能重复出现)的某列的值之和*/
public static HashMap<String,Double> getItemsValueSums(String fitem,String fdata,int tarIndex,int dataIndex){
ArrayList<String> targets=FileUtil.readFileByRow(fitem);
ArrayList<String[]> rows=ExcelUtil.readRows(fdata, 0, 2);//默认0sheet、3行开始
HashMap<String,Double> hm =new HashMap<String, Double>();
for(String[] arr:rows){
String item=arr[tarIndex];
for(String tar:targets){
if(item.contains(tar)){
UT.addKV(tar, Double.parseDouble(arr[dataIndex]),hm);
}
}
}
return hm;
}
public static ArrayList<Cell> getRowCells(Sheet sheet,int rowIn,int[] colIns){
ArrayList<Cell> cells=new ArrayList<Cell>();
Cell[] row=sheet.getRow(rowIn);
for(int i=0;i<colIns.length;i++){
int index=colIns[i];
if(index<row.length)//index不能大于当前row的长度(//一般为表头表尾非数据行)
cells.add(row[index]);
}
return cells;
}
public static String[] getStringArrOfRow(Cell[] cells){
if(cells==null||cells.length==0)
return null;
String[] arr = new String[cells.length];
for (int i=0;i<cells.length;i++) {
arr[i]= cells[i].getContents();
}
return arr;
}
public static String[] getStringArrOfRow(ArrayList<Cell> cells){
if(cells==null||cells.size()==0)
return null;
String[] arr = new String[cells.size()];
for (int i=0;i<cells.size();i++) {
arr[i]= cells.get(i).getContents();
}
return arr;
}
/**表格类型:0-xls;1-xlsx*/
public static int EType(String path){
if(path.endsWith(".xls"))
return 0;
return 1;
}
/**根据数据行的长度获取表格数据行的起始位置*/
public static int getStartRow(Sheet sheet,int dataRowLen){
int index=0;
for(int i=0;i<sheet.getRows();i++){
Cell[] row=sheet.getRow(i);
if(row==null||row.length==0)continue;
// int len=row.length;
int len=getLastNotnullCell(row);
if(dataRowLen==len){
return i;
}
}
return index;
}
/**因为即使一格的合并单元格也算表格长度,剩余的全部是"",而我们需要的长度是实际内容单元格的个数,所以根据最后一个有内容的单元格来确定实际长度*/
public static int getLastNotnullCell(Cell[] row){
int len=0;
String[] arr=getStringArrOfRow(row);
for(int j=arr.length-1;j>-1;j--){
String content=arr[j];
if(content.length()>0)
return j+1;
}
return len+1;//一行至少有一格吧
}
}