import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class Main{
public static void main(String[] args) throws Exception {
File file = new File("123.xls");
getData(file, 1);
}
/**
* 读取Excel的内容,第一维数组存储的是一行中格列的值,二维数组存储的是多少个行
* @param file 读取数据的源Excel
* @param ignoreRows 读取数据忽略的行数,比喻行头不需要读入 忽略的行数为1
* @return 读出的Excel中数据的内容
* @throws FileNotFoundException
* @throws IOException
*/
public static void getData(File file, int ignoreRows)
throws FileNotFoundException, IOException {
List<String[]> result = new ArrayList<String[]>();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
// 打开HSSFWorkbook
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell checker = null;
HSSFCell filePath = null;
HSSFCell lineNmuber = null;
HSSFCell function = null;
// 循环工作表Sheet
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
HSSFSheet st = wb.getSheetAt(sheetIndex);
HSSFRow row0 = st.getRow(0);
HSSFCell tempCell = row0.createCell((short) 25);
tempCell.setCellValue("SUCCESS");
// 第一行为标题,不取
// 循环行Row
for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
//添加最后一列
HSSFCell newCell = row.createCell((short)25);
checker = row.getCell((short) 4);
String checkerValue = null;
String pathNamme = null;
if (checker == null) {
return;
}
// 注意:一定要设成这个,否则可能会出现乱码
checker.setEncoding(HSSFCell.ENCODING_UTF_16);
if (checker.getCellType() == HSSFCell.CELL_TYPE_STRING) {
checkerValue = checker.getStringCellValue();
}
if(null == checkerValue && !checkerValue.equals("OVER"))
{
return;
}
filePath = row.getCell((short) 14);
if (filePath == null) {
return;
}
filePath.setEncoding(HSSFCell.ENCODING_UTF_16);
String filePathValue = null;
if (filePath.getCellType() == HSSFCell.CELL_TYPE_STRING) {
filePathValue = filePath.getStringCellValue();
}
filePathValue.replace(".","\\");
pathNamme = pathNamme + filePathValue;
System.out.println("pathNamme"+pathNamme);
lineNmuber = row.getCell((short) 25);
if (lineNmuber == null) {
return;
}
lineNmuber.setEncoding(HSSFCell.ENCODING_UTF_16);
int line = 0;
if (lineNmuber.getCellType() == HSSFCell.CELL_TYPE_STRING) {
line = Integer.valueOf(lineNmuber.getStringCellValue()).intValue();
System.out.println("line"+line);
}
function = row.getCell((short) 15);
if (function == null) {
return;
}
function.setEncoding(HSSFCell.ENCODING_UTF_16);
String[] functionName1 = null;
String[] functionName2 = null;
String funName = null;
if (function.getCellType() == HSSFCell.CELL_TYPE_STRING) {
functionName1 = function.getStringCellValue().split("\\.");
}
functionName2 = functionName1[functionName1.length-1].split("\\(");
funName = functionName2[0];
System.out.println("funName"+funName);
FileReader fr = new FileReader(pathNamme);
FileWriter fw = new FileWriter("temp.txt");
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
int i=0;
String str = null;
while((str = br.readLine()) != null){
//读取文件当中的一行
i++;
if(i == line && str.contains(funName)){
if (str.contains("public"))
{
str.replace("public", "public final");
}else if(str.contains("protect"))
{
str.replace("protect", "protect final");
}
}
bw.newLine();
bw.write(str);
}
br.close();
bw.close();
FileReader fr2 = new FileReader("temp.txt");
FileWriter fw2 = new FileWriter(pathNamme);
BufferedReader br2 = new BufferedReader(fr2);
BufferedWriter bw2 = new BufferedWriter(fw2);
String str2 = null;
while((str2 = br2.readLine()) != null){
//读取文件当中的一行
bw2.write(str2);
}
newCell.setCellValue("YES");
br2.close();
bw2.close();
}
}
in.close();
}
/**
* 去掉字符串右边的空格
* @param str 要处理的字符串
* @return 处理后的字符串
*/
public static String rightTrim(String str) {
if (str == null) {
return "";
}
int length = str.length();
for (int i = length - 1; i >= 0; i--) {
if (str.charAt(i) != 0x20) {
break;
}
length--;
}
return str.substring(0, length);
}
}
文件
最新推荐文章于 2024-05-06 10:04:03 发布