public class WriteToExcel {
/ Initialize logger object
Logger log = Logger.getLogger(WriteToExcel.class.getName());
public void write(Map> preparedData, String fileName) {
// Define a workbook and a sheet for the report`enter code here`
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("REPORT");
Properties properties = new Properties();
InputStream inputStream = null;
try {
Set keySet = preparedData.keySet();
List list = new ArrayList(keySet);
// Read config.properties
inputStream = new FileInputStream("./config.properties");
properties.load(inputStream);
System.out.println("inside excel Collections in" );
// Sort the list so that heading row will come as first row
Collections.sort(list);
System.out.println("inside excel Collections out" );
int rowNum = 0;
int count1 =0, count2 =0;
// Add data to the workbook
for (String key : list) {
System.out.println("inside excel string key"+ ++count1 );
Row row = sheet.createRow(rowNum++);
List objectArray = preparedData.get(key);
int cellNum = 0;
for (Object obj : objectArray) {
System.out.println("inside excel object for loop "+ ++count2 );
Cell cell = row.createCell(cellNum++);
if (obj instanceof Date)
cell.setCellValue((Date) obj);
else if (obj instanceof Integer)
cell.setCellValue((Integer) obj);
else if (obj instanceof String)
cell.setCellValue((String) obj);
else if (obj instanceof Double)
cell.setCellValue((Double) obj);
}
}
FileOutputStream fileOutputSTream;
Calendar todaysDate = Calendar.getInstance();
String todaysFormattedDate = Util.formatDateUnderscore(todaysDate);
try {
String filePath = properties.getProperty("filePath");
String completeFilename = filePath + fileName + "_"
+ todaysFormattedDate + ".xls";
fileOutputSTream = new FileOutputStream(new File(
completeFilename));
workbook.write(fileOutputSTream);
fileOutputSTream.close();
} catch (FileNotFoundException e) {
log.error("FileNotFoundException : ", e);
}
} catch (Exception e) {
log.error("Exception : ", e);
}
}
}
寻求帮助确定代码循环的错误;为什么这么长时间/造成这个问题?