将数据库的数据导出到EXCEL中

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ExportData(String[][] s,String outputFile){

//  创建新的Excel
  HSSFWorkbook workBook = new HSSFWorkbook();
  //创建工作表
  HSSFSheet sheet = workBook.createSheet();  
  //在索引0的位置创建行(最顶端的行)
  HSSFRow row = sheet.createRow((short) 0);
  HSSFCell cell = null; 
  //内容行样式
  HSSFCellStyle conStyle = workBook.createCellStyle();
  try{

for(int i=0;i<s.length ;i++){
     row = sheet.createRow(i);

cell = row.createCell((short)j);
     cell.setCellStyle(conStyle); 

HSSFCellStyle conStyle1 = workbook.createCellStyle();
       conStyle1.setFillForegroundColor(HSSFColor.BLUE.index);//可对指定单元格背景变色
       conStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
       cell.setCellStyle(conStyle1);

cell.setCellValue(Tools.isNull(s[i][j]));

}

//新建一输出文件流
   File oldFile = new File(outputFile);
   if (oldFile.exists()) {
    oldFile.delete();
   }
   FileOutputStream fOut = new FileOutputStream(outputFile);
   // 把相应的Excel 工作簿存盘

   workbook.write(fOut);
   fOut.flush();
   // 操作结束,关闭文件
   fOut.close();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值