JAva Excelファイルのエクスポート
1477 ワード
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public void exportExcel(List list, String fileName, Map headName) throws WriteException,IOException{
OutputStream os = new FileOutputStream(new File(fileName));
//
WritableWorkbook workbook = Workbook.createWorkbook(os);
//
WritableSheet sheet = workbook.createSheet(" ",0);
//
Map dataMap;
int row = 0, cell = 0;
if(headName == null){
headName = (Map)list.get(0);
}
for(String key : headName.keySet()){
Label label = new Label(cell, row ,headName.get(key));
sheet.addCell(label);
cell++;
}
//
for(row = 0;row < list.size();row++){
cell = 0;
dataMap = (Map)list.get(row);
for(String key : headName.keySet()){
Label label = new Label(cell, row + 1 ,dataMap.get(key).toString());
sheet.addCell(label);
cell++;
}
}
workbook.write();
workbook.close();
os.close();
}