1.使用poi以及dom4j读取一个有4000多条xml格式的excel如下:
需要取出具体几个节点里面的值然后输出到一个txt中,代码运行出现StringIndexOutOfBoundsException错误。写出的txt中有几百条正确数据,代码如下:
2.代码
public class Main {
public static void main(String[] args) throws ParserConfigurationException, SAXException, DocumentException, IOException {
String sr[]={"<ns2:uniqueKey>","<ns3:tmId>","<ns3:status>","<ns3:volume>","<ns3:weight>","<ns3:orderId>"};
String sr2[]={"</ns2:uniqueKey>","</ns3:tmId>","</ns3:status>","</ns3:volume>","</ns3:weight>","</ns3:orderId>"};
String filePath = "C:\\Users\\sun.song\\Desktop\\test.xlsx";
DaiPoi.loadScoreInfo(filePath,sr,sr2);
}
}
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.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.Iterator;
import java.util.List;
/**
* Created by sun.song on 2016/12/7.
*/
public class DaiPoi {
public static void loadScoreInfo(String xlsPath,String[] first,String[] last) throws IOException, ParserConfigurationException, SAXException, DocumentException {
FileWriter writer=null;
FileInputStream fileIn = new FileInputStream(xlsPath);
//根据指定的文件输入流导入Excel从而产生Workbook对象
Workbook wb0 = new XSSFWorkbook(fileIn);
//获取Excel文档中的第一个表单
Sheet sht0 = wb0.getSheetAt(0);
//对Sheet中的每一行进行迭代
for (Row r : sht0) {
for (Cell c : r) {
if (first.length == last.length) {
String sub = handleString(c, first, last);
writer = new FileWriter("d:/2.txt", true);
//System.out.println("添加了一条");
writer.write(sub);
writer.close();
}
//System.out.println("111");
writer = new FileWriter("d:/2.txt", true);
writer.write("\n");
writer.close();
}
fileIn.close();
}
}
public static String handleString(Cell c,String[] first,String[] last) {
StringBuffer sb= new StringBuffer("");
for (int i = 0; i < first.length; i++) {
String firstString = first[i];
//System.out.println(firstString);
String lastString = last[i];
int firstStringLength = firstString.length();
String cellValue = c.getStringCellValue();
StringBuffer bufferCellValue = new StringBuffer(cellValue);
// System.out.println(cellValue);
int firstStringIndex = cellValue.indexOf(firstString);
int lastStringIndex = cellValue.indexOf(lastString);
StringBuffer subs = new StringBuffer(";"+bufferCellValue.substring(firstStringLength + firstStringIndex, lastStringIndex));
sb.append(subs);
}
return sb.toString();
}
}
3.根据报错看出是角标超长了,但是一直没找出原因,球大神们帮助
Anda tidak menyatakan pada baris mana ralat itu muncul, jadi saya hanya meneka ia ada di sini.
StringBuffer subs = new StringBuffer(";"+bufferCellValue.substring(firstStringLength + firstStringIndex, lastStringIndex));
Apabila firstString tidak ditemui, -1 dikembalikan Jika firstString ialah "", jumlahnya adalah -1, dan ralat ini akan dilaporkan.
Mungkin ada situasi lain, yang saya tidak dapat menguji buat masa ini.