使用java怎么导出Excel

  介绍

这期内容当中小编将会给大家带来有关使用java怎么导出Excel,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

首先需要引入的jar包:

使用java怎么导出Excel“> </p> <p>然后就是正式代码了。</p> <pre类= package  lcy._41_50;   ,   import  java.io.FileOutputStream;   import  java.io.OutputStream;   import  java.net.URLEncoder;   ,   import  javax.servlet.http.HttpServletResponse;   ,   import  org.apache.poi.hssf.usermodel.HSSFCell;   import  org.apache.poi.hssf.usermodel.HSSFCellStyle;   import  org.apache.poi.hssf.usermodel.HSSFFont;   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.CellRangeAddress;   import  org.apache.poi.hssf.util.HSSFColor;   ,   @SuppressWarnings (, {,“deprecation",})   public  class  Test46  {   ,   public  static  void  main (String [], args), throws  Exception  {   ,   String  sheetName =,“用车统计表单“;   String  titleName =,“用车申请数据统计表“;   String  fileName =,“用车申请统计表单“;   int  columnNumber =, 3;   int [], columnWidth =,{, 10, 20日,30日,};   String [] [], dataList =,{,{,“001”,,,“2015 - 01 - 01 -“,,,你,,},   {,“002”,,,“2015 - 01 - 02 -“,,“市场部“,},,{,“003“,,,,2015 - 01 - 03 -“,,“测试,,},};   String [], columnName =,{,“单号”,,“申请时间,,,“申请部门“,};   new  Test46 () .ExportNoResponse (sheetName, titleName,,文件名,   columnNumber, columnWidth, columnName,, dataList);   }   ,   public  void  ExportWithResponse (String  sheetName, String  titleName,   文件名,String  int  columnNumber, int [], columnWidth,   columnName, String [], String [] [], dataList,   HttpServletResponse 响应),throws  Exception  {   if  (columnNumber ==, columnWidth.length&,, columnWidth.length ==, columnName.length), {//,第一步,创建一个webbook,对应一个Excel文件   HSSFWorkbook  wb =, new  HSSFWorkbook ();//,第二步,在webbook中添加一个表,对Excel应文件中的表   HSSFSheet  sheet =, wb.createSheet (sheetName);//,sheet.setDefaultColumnWidth(15),,//统一设置列宽   for  (int 小姐:=,0;,小姐:& lt;, columnNumber;,我+ +),   {   时间=for  (int  j  0;, j  & lt;=,我。,j + +),   {   if (小姐:==,j),   {   sheet.setColumnWidth(我,columnWidth [j], *, 256),,//,单独设置每列的宽   }   }   }//,创建第0行,也就是标题   HSSFRow  row1 =, sheet.createRow ((int), 0);   row1.setHeightInPoints(50);//,设备标题的高度//,第三步创建标题的单元格样式style2以及字体样式headerFont1   HSSFCellStyle  style2 =, wb.createCellStyle ();   style2.setAlignment (HSSFCellStyle.ALIGN_CENTER);   style2.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);   style2.setFillForegroundColor (HSSFColor.LIGHT_TURQUOISE.index);   style2.setFillPattern (HSSFCellStyle.SOLID_FOREGROUND);   HSSFFont  headerFont1 =, (HSSFFont), wb.createFont();,//,创建字体样式   headerFont1.setBoldweight (HSSFFont.BOLDWEIGHT_BOLD);,//,字体加粗   headerFont1.setFontName(“黑体“),//,设置字体类型   headerFont1.setFontHeightInPoints((短),15);,//,设置字体大小   style2.setFont (headerFont1);,//,为标题样式设置字体样式   ,   HSSFCell  cell1 =, row1.createCell(0);//,创建标题第一列   sheet.addMergedRegion (new  CellRangeAddress (0, 0, 0,   columnNumber 作用;1));,//,合并列标题   cell1.setCellValue (titleName);,//,设置值标题   cell1.setCellStyle (style2);,//,设置标题样式   ,//,创建第1行,也就是表头   HSSFRow  row =, sheet.createRow ((int), 1);   row.setHeightInPoints(37);//,设置表头高度   ,//,第四步,创建表头单元格样式,以及表头的字体样式   HSSFCellStyle  style =, wb.createCellStyle ();   style.setWrapText(真正);//,设置自动换行   style.setAlignment (HSSFCellStyle.ALIGN_CENTER);   style.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);,//,创建一个居中格式   ,   style.setBottomBorderColor (HSSFColor.BLACK.index);   style.setBorderBottom (HSSFCellStyle.BORDER_THIN);   style.setBorderLeft (HSSFCellStyle.BORDER_THIN);   style.setBorderRight (HSSFCellStyle.BORDER_THIN);   style.setBorderTop (HSSFCellStyle.BORDER_THIN);   ,   HSSFFont  headerFont =, (HSSFFont), wb.createFont();,//,创建字体样式   headerFont.setBoldweight (HSSFFont.BOLDWEIGHT_BOLD);,//,字体加粗   headerFont.setFontName(“黑体“),//,设置字体类型   headerFont.setFontHeightInPoints((短),10);,//,设置字体大小   style.setFont (headerFont);,//,为标题样式设置字体样式   ,//,第四。一步,创建表头的列   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

使用java怎么导出Excel