我写了这个程序,遍历每一行并尝试查找特定数据然后根据它,找到特定的xml文件并提取数据并将其上传到excel表。 excel表大约有90,000行,所以我可以理解为什么代码可能会变慢。但它似乎太慢了,它在1小时内更新了大约5000行,并且在90,000行中更新;它将需要永远。 我上传了xml bean并将-Xmx设为2048m,因此它不是内存问题。当我将参数更改为1到100时,它确实更新了;所以我肯定知道代码是有效的,但它太慢了。 有什么建议吗?
import java.util.*;
import java.io.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XMLRead {
public static void main(String[] Args) throws Exception{
FileInputStream fis = new FileInputStream(new File("C:/Desktop/TestECG.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheet("ecg");
for (int i = 1; i <= 91332; i++){
if(sheet.getRow(i).getCell(2) == sheet.getRow(i-1).getCell(2) && sheet.getRow(i).getCell(15) == sheet.getRow(i-1).getCell(15) && sheet.getRow(i).getCell(16) == sheet.getRow(i-1).getCell(16)) {
XSSFCell lame = sheet.getRow(i-1).getCell(19);
String lamer = lame.toString();
sheet.getRow(i).getCell(19).setCellValue(lamer);
}
else{
XSSFCell TimeCell = sheet.getRow(i).getCell(16);
String TimeString = TimeCell.toString();
String gettime[] = TimeString.split(":");
String TimeHour = gettime[0];
String TimeMinute = gettime[1];
String TimeSecond = gettime[2];
XSSFCell DateCell = sheet.getRow(i).getCell(15);
String DateString = DateCell.toString();
String DateYear = DateString.substring(5, 9);
String TempDateMonth = DateString.substring(2,5);
String DateMonth = "";
if (TempDateMonth.equals("Jan")){
DateMonth = "01";
}
else if (TempDateMonth.equals("Feb")){
DateMonth = "02";
}
else if (TempDateMonth.equals("Mar")){
DateMonth = "03";
}
else if (TempDateMonth.equals("Apr")){
DateMonth = "04";
}
else if (TempDateMonth.equals("May")){
DateMonth = "05";
}
else if (TempDateMonth.equals("Jun")){
DateMonth = "06";
}
else if (TempDateMonth.equals("Jul")){
DateMonth = "07";
}
else if (TempDateMonth.equals("Aug")){
DateMonth = "08";
}
else if (TempDateMonth.equals("Sep")){
DateMonth = "09";
}
else if (TempDateMonth.equals("Oct")){
DateMonth = "10";
}
else if (TempDateMonth.equals("Nov")){
DateMonth = "11";
}
else if (TempDateMonth.equals("Dec")){
DateMonth = "12";
}
String DateDay = "";
String TempDateDay = DateString.substring(0, 2);
if (Integer.parseInt(TempDateDay.trim())< 10){
DateDay = "0" + TempDateDay;
}
else if (Integer.parseInt(TempDateDay.trim()) >= 10){
DateDay = TempDateDay;
}
String CombineLowValue = DateYear + DateMonth + DateDay + TimeHour + TimeMinute + TimeSecond;
XSSFCell SubjIDCell = sheet.getRow(i).getCell(2);
String SUBID = SubjIDCell.toString();
File ECGDirectory = new File("C:/Users/ahmeda/Documents/ECG/");
String[] names = ECGDirectory.list();
String RightXML = null;
String getTheName = null;
searchloop: for(String name: names){
if (new File("C:/Documents/ECG/" + name).isDirectory())
{
RightXML = ListFiles("C:/Documents/ECG/" + name + "/",SUBID, CombineLowValue);
if (RightXML != null){
getTheName = name;
break searchloop;
}
}
}
if(getTheName != null && RightXML != null){
File readXMLfile = new File("C:/Users/ahmeda/Documents/ECG/" + getTheName + "/" + RightXML);
Scanner read = new Scanner(readXMLfile);
int onlyoneIDroot = 0;
int onlyonelowvalue = 0;
int onlyoneidextension = 0;
String searchidroot = "id root";
String searchlowvalue = "low value";
String searchidextension = "id extension";
while (read.hasNextLine()){
String readline = read.nextLine();
if (readline.indexOf(searchidroot.toLowerCase()) != -1 && onlyoneIDroot != 1){
String getid[] = readline.split("=");
String almost[] = getid[1].split("\"");
searchidroot = almost[1];
onlyoneIDroot++;
}
else if (readline.indexOf(searchlowvalue.toLowerCase()) != -1 && onlyonelowvalue != 1){
String getid[] = readline.split("=");
String almost[] = getid[1].split("\"");
searchlowvalue = almost[1];
onlyonelowvalue++;
}
else if (readline.indexOf(searchidextension.toLowerCase()) != -1 && onlyoneidextension != 1){
String getid[] = readline.split("=");
String almost[] = getid[1].split("\"");
searchidextension = almost[1];
onlyoneidextension++;
}
}
if(SUBID.equals(searchidextension) && CombineLowValue.equals(searchlowvalue)){
sheet.getRow(i).getCell(19).setCellValue(searchidroot);
System.out.println("I am right here, just to see if this is even working.");
}
read.close();
}
}
}
fis.close();
FileOutputStream fos =new FileOutputStream(new File("C:/Users/ahmeda/Desktop/TestECG.xlsx"));
workbook.write(fos);
workbook.close();
fos.close();
System.out.println("Done");
}
static String ListFiles(String file, String ID, String LowValue) {
String files = null;
File folder = new File(file);
File[] listOfFiles = folder.listFiles();
//int x = 0;
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
String getDate[] = listOfFiles[i].getName().split("_");
String getID = getDate[2].substring(0,4);
//System.out.println(listOfFiles[i].getName().split("_"));
//System.out.println(getDate[2]);
/*System.out.println(getID);
System.out.println(ID);
System.out.println(getDate[3]);
System.out.println(LowValue);*/
if(getID.equals(ID) && getDate[3].equals(LowValue)){
files = listOfFiles[i].getName();
//System.out.println(files);
return files;
}
}
}
return files;
}
}