//alarmLogExport.action
/**
* 日志导出
*
*/
@SuppressWarnings("unchecked")
public String alarmLogExport() throws IOException {
try {
// AlarmLog 是model
//数据库查询数据放到List
List<AlarmLog> list = alarmLogService.getAlarmLogList(beginTime, endTime,alarmMode, result);
} catch (Exception e1) {
}
response = (HttpServletResponse) ServletActionContext.getResponse();
OutputStream os = null;
try {
os = response.getOutputStream();
} catch (IOException e) {
}
response.reset();
response.setHeader("Content-disposition", "p_w_upload; filename="
+ "alarmLog.xls");
response.setContentType("application/msexcel");
LogExport excelExport = new LogExport();
inputStream = excelExport.exportAlarmLog(list, os);
return SUCCESS;
}
//工具类
//LogExport.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.model. AlarmLog;
public class LogExport {
//警报日志AlarmLog
public InputStream exportAlarmLog(List<AlarmLog> alarmLog, OutputStream os) throws IOException {
//SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//创建excel
HSSFWorkbook hwb = new HSSFWorkbook();
int k = 1;
//创建sheet页
HSSFSheet sheet = hwb.createSheet("警报日志" + k);
k++;
//创建标题行
HSSFRow row1 = sheet.createRow(0);
row1.createCell(0).setCellValue(new HSSFRichTextString("通知的用户名"));
row1.createCell(1).setCellValue(new HSSFRichTextString("通知的用户组织"));
row1.createCell(2).setCellValue(new HSSFRichTextString("通知的用户邮箱"));
row1.createCell(3).setCellValue(new HSSFRichTextString("通知的用户电话"));
row1.createCell(4).setCellValue(new HSSFRichTextString("警报通知时间"));
row1.createCell(5).setCellValue(new HSSFRichTextString("警报内容"));
int row = 0;
for (int i = 0; i < alarmLog.size(); i++) {
if (i > 0 && i % 10000 == 0) {
sheet = hwb.createSheet("警报日志" + k); // 建立新的BirthSheet对象
k++;
row1.createCell(0).setCellValue(new HSSFRichTextString("通知的用户名"));
row1.createCell(1).setCellValue(new HSSFRichTextString("通知的用户组织"));
row1.createCell(2).setCellValue(new HSSFRichTextString("通知的用户邮箱"));
row1.createCell(3).setCellValue(new HSSFRichTextString("通知的用户电话"));
row1.createCell(4).setCellValue(new HSSFRichTextString("警报通知时间"));
row1.createCell(5).setCellValue(new HSSFRichTextString("警报内容"));
}
HSSFRow row2 = sheet.createRow((row % 10000) + 1);// 创建表格的行
row2.createCell(0).setCellValue(new HSSFRichTextString(alarmLog.get(i).getUserName()));
row2.createCell(1).setCellValue(new HSSFRichTextString(alarmLog.get(i).getUserDepart()));
row2.createCell(2).setCellValue(new HSSFRichTextString(alarmLog.get(i).getUserEmail()));
row2.createCell(3).setCellValue(new HSSFRichTextString(alarmLog.get(i).getUserTelephone()));
row2.createCell(4).setCellValue(new HSSFRichTextString(df.format(alarmLog.get(i).getAlarmTime())));
row2.createCell(5).setCellValue(new HSSFRichTextString(alarmLog.get(i).getAlarmContent()));
row++;
}
try {
hwb.write(os);
os.flush();
return new ByteArrayInputStream(hwb.getBytes());
} catch (IOException e) {
return null;
}
}
}
到此就成功导出了。