文章目录
1.读取excel文件2.利用StringBuffer组装数据
1.读取excel文件
public StringBuffer
readExcel(Map map
){
XSSFWorkbook wb
= null
;
if (null
!= map
.get("xlsxName")) {
String xlsxName
= map
.get("xlsxName");
try{
wb
= new XSSFWorkbook(this.getClass().getResourceAsStream(xlsxName
));
} catch (IOException e
) {
LOG
.error("readExcel_error" + e
.getMessage
, e
);
}
}
Sheet sheet
= wb
.getSheet("sheet1");
Row row
= null
;
Row firstRow
= null
;
StringBuffer sb
= new StringBuffer();
for (int i
= 1; i
< sheet
.getLastRowNum() + 1; i
++) {
try{
if(sheet
.getRow(i
) == null
|| sheet
.getRow(i
).getCell(0) == null
) {
continue;
}
firstRow
= sheet
.get(0);
for (
int j
= 0; j
< first
.getLastCellNum(); j
++)
{
row
= sheet
.getRow(i
);
cell
= row
.getCell(j
);
if (cell
!= null
) {
sb
.append(cell
);
} else {
sb
.append("");
}
}
sb
.append(System
.getProperty("line.separator"));
if(i
% 2000 == 0)
{
assembleTxtData(sb
);
sb
.delete(0, sb
.length());
}
} catch (Exception e
) {
LOG
.error("readExcel_error" + e
.getMessage(), e
);
}
}
assembleTxtData(sb
);
}
2.利用StringBuffer组装数据
public void assembleTxtData(StringBuffer sb
){
FileOutputStream fos
= null
;
PrintWriter pw
= null
;
File file
= new File("D://1.txt");
try{
if (!file
.exists()) {
file
.createNewFile();
}
fos
= new FileOutputStream(file
, true);
pw
= new PrintWriter(fos
);
pw
.write(sb
.toString().toCharArray());
pw
.flush();
} catch (IOException e
) {
LOG
.error("assembleTxtData_error" + e
.getMessage(), e
);
} finally {
if (pw
!= null
) {
pw
.close();
}
if (fos
!= null
) {
try {
fos
.close();
} catch (IOException e
) {
LOG
.error("assembleTxtData_error" + e
.getMessage(), e
);
}
}
}
}
转载请注明原文地址:https://tech.qufami.com/read-16294.html