java导出excel表格进行判断和时间日期格式设置

您所在的位置:网站首页 导入数据到cad日期格式不对 java导出excel表格进行判断和时间日期格式设置

java导出excel表格进行判断和时间日期格式设置

2023-08-12 00:40| 来源: 网络整理| 查看: 265

后端代码实现例子:

在这里我定义一个 content

我获取的数据库值就会从0开始对应

list是数据库调用数据的定义值

具体例子判断和日期格式定义成yyyy--mm-dd例子如:

@ResponseBody

@RequestMapping("/exportExcel")     public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {

...

List list = getWorkerService().getExportWorkers(weParam);

String title = "xxxx";

String[] Mytitle = {"姓名", "性别",''创建日期''};

String sheetName = "xxx档案";

int a = list.size(); String content[][] = new String[a][];

for (int i = 0; i < list.size(); i++) {

content[i] = new String[Mytitle.length];

content[i][0] = list.get(i).getChineseName();

content[i][1] = list.get(i).getSex() == 0 ? "男" : "女";

content[i][2] = String.valueOf(DateUtil.format(list.get(i).getCreateDate(), DatePattern.NORM_DATE_FORMATTER));

}

//创建HSSFWorkbook HSSFWorkbook wb = ExportExcelUtil.getHSSFWorkbook(sheetName, Mytitle, title, content, null);

try {             String name = "xxxx.xls";      //  文件名             OutputStream os = response.getOutputStream();  // 取得输出流             response.reset(); // 清空输出流             response.setHeader("Content-Disposition", "attachment;filename=" + name + ";filename*=utf-8''"                     + URLEncoder.encode(name, "UTF-8"));  // 设定输出文件头             response.setContentType("application/msexcel");// 定义输出类型             wb.write(os);             os.flush();             os.close();         } catch (Exception e) {             e.printStackTrace();         }     }

注意: 1.ExportExcelUtil是一个导出的excel格式定义,例子:

            org.meteor             meteor-mgr             0.0.1.2-SNAPSHOT

具体代码:

package org.meteor.mgr.utils;

import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; 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;

@SuppressWarnings("deprecation") public class ExportExcelUtil {      /**       *   * 导出Excel       * @param sheetName sheet名称       * @param titleColumn 列标题       * @param title 标题       * @param values 表内容       * @param wb  HSSFWorkbook对象       * @return       */     public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []titleColumn,String title,String [][]values, HSSFWorkbook wb){

        // 第一步,创建一个HSSFWorkbook,对应一个Excel文件         if(wb == null){             wb = new HSSFWorkbook();         }

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet         HSSFSheet sheet = wb.createSheet(sheetName);         //创建表名的样式         HSSFCellStyle tableNamestyle = wb.createCellStyle();         tableNamestyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式         sheet.autoSizeColumn((short)0);         tableNamestyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框         tableNamestyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框         tableNamestyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框         tableNamestyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框         //设置字体         HSSFFont tableNamefont =wb.createFont();         tableNamefont.setFontHeightInPoints((short) 14); //字体高度         tableNamefont.setColor(HSSFFont.COLOR_NORMAL); //字体颜色         tableNamefont.setFontName("宋体"); //字体         tableNamefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度         tableNamestyle.setFont(tableNamefont);                           //创建第一行         HSSFRow row = sheet.createRow(0);         //设置合并单元格样式         setCellBorder(1,titleColumn.length-1,row,tableNamestyle);         //单元格合并         CellRangeAddress cra =new CellRangeAddress(0, 0, 0, titleColumn.length-1);         sheet.addMergedRegion(cra);         //声明列对象         HSSFCell cell = null;         cell=row.createCell(0);         cell.setCellValue(title);         /**设置单元格格式为文本格式*/         HSSFDataFormat format = wb.createDataFormat();         tableNamestyle.setDataFormat(format.getFormat("@"));         cell.setCellStyle(tableNamestyle);//设置单元格格式为"文本"         cell.setCellType(HSSFCell.CELL_TYPE_STRING);

                          // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制         row = sheet.createRow(1);

        // 第四步,创建单元格,并设置值表头 设置表头居中         HSSFCellStyle titlestyle = wb.createCellStyle();         titlestyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式         sheet.autoSizeColumn((short)0);         //设置字体         HSSFFont font =wb.createFont();         font.setFontHeightInPoints((short) 9); //字体高度         font.setColor(HSSFFont.COLOR_NORMAL); //字体颜色         font.setFontName("宋体"); //字体         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度         titlestyle.setFont(font);         titlestyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框         titlestyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框         titlestyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框         titlestyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框                                     //创建单元格样式         HSSFCellStyle cellstyle = wb.createCellStyle();         //设置字体         HSSFFont tableCountfont =wb.createFont();         tableCountfont.setFontHeightInPoints((short) 10); //字体高度         tableCountfont.setColor(HSSFFont.COLOR_NORMAL); //字体颜色         tableCountfont.setFontName("宋体"); //字体         tableCountfont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); //宽度         cellstyle.setFont(tableCountfont);                  cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式         cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框         cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框         cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框         cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

        /**设置单元格格式为文本格式*/         HSSFDataFormat format1 = wb.createDataFormat();         cellstyle.setDataFormat(format1.getFormat("@"));                                    //创建标题         for(int i=0;i              HSSFCell cellcell = null;             row = sheet.createRow(i + 2);             for(int j=0;j             sheet.autoSizeColumn(k);             HSSFCellStyle cellstyle1 = wb.createCellStyle();             HSSFDataFormat format2 = wb.createDataFormat();             cellstyle1.setDataFormat(format2.getFormat("@"));             sheet.setDefaultColumnStyle(k, cellstyle1);         }         setSizeColumn(sheet, values[0].length);         return wb;     }

     /**       * 自适应宽度(中文支持)       * @param sheet       * @param size       */      public static void setSizeColumn(HSSFSheet sheet, int size) {

         for (int columnNum = 0; columnNum < size; columnNum++) {              int columnWidth = sheet.getColumnWidth(columnNum) / 256;              for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {                  HSSFRow currentRow;                  //当前行未被使用过                  if (sheet.getRow(rowNum) == null) {                      currentRow = sheet.createRow(rowNum);                  } else {                      currentRow = sheet.getRow(rowNum);                  }                  if (currentRow.getCell(columnNum) != null) {                      HSSFCell currentCell = currentRow.getCell(columnNum);                      if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {                          int length = currentCell.getStringCellValue().getBytes().length;                          if (columnWidth < length) {                              columnWidth = length;                          }                      }                  }              }              sheet.setColumnWidth(columnNum, columnWidth * 256);          }      }      /**          * 合并单元格加边框  水平          * @param sheet          * @param region          * @param cs       * 参数说明:start和并的第二列,end为合并的最后一列,row就为当前行,style样式(里面有设置边框)         例如从0-10列合并: ExcelUtil.setCellBorder(2,10,row,style);   这样可以设置          */           public static void setCellBorder(int start, int end, HSSFRow row,    HSSFCellStyle style) {               for(int i=start;i



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3