public synchronized static int writeExcel(JSONArray jsonArray,int k) {
//创建Excel文件薄
HSSFWorkbook workbook=new HSSFWorkbook();
//创建工作表sheeet
HSSFSheet sheet=workbook.createSheet();
//创建第一行
HSSFRow row=sheet.createRow(0);
String[] title={"货号","中文名","英文名","CAS","分子式","分子量","图片网址","svg网址"};
HSSFCell cell_title = null;
for (int i=0;i<title.length;i++){
cell_title=row.createCell(i);
cell_title.setCellValue(title[i]);
}
//追加数据
for (int i=0;i<jsonArray.size();i++){
JSONObject jsonObject =(JSONObject) jsonArray.get(i);
HSSFRow nextrow=sheet.createRow(i+1);
HSSFCell cell_value=nextrow.createCell(0);
cell_value.setCellValue("");
cell_value=nextrow.createCell(1);
cell_value.setCellValue(TransApi.EN_to_ZH(jsonObject.get("name").toString()));
cell_value=nextrow.createCell(2);
cell_value.setCellValue(jsonObject.get("name").toString());
cell_value=nextrow.createCell(3);
cell_value.setCellValue(jsonObject.get("CAS").toString());
cell_value=nextrow.createCell(4);
cell_value.setCellValue(jsonObject.get("FCS").toString());
cell_value=nextrow.createCell(5);
cell_value.setCellValue(jsonObject.get("MW").toString());
cell_value=nextrow.createCell(6);
cell_value.setCellValue(jsonObject.get("img").toString());
cell_value=nextrow.createCell(7);
cell_value.setCellValue(jsonObject.get("svg").toString());
}
//创建一个文件
try {
File file=new File("D:/pacong/poi_test_"+k+".xls");
file.createNewFile();
FileOutputStream stream= FileUtils.openOutputStream(file);
workbook.write(stream);
stream.close();
}catch (Exception e){
e.printStackTrace();
}finally {
return 1;
}
}