zoukankan      html  css  js  c++  java
  • java导出统计数据excel设置单元格样式

    什么也不说先上效果图:

    一、在后台实现,利用java的poi
    1、导入jar包,需要导入lib文件夹下如下包:
    poi-3.11-20141221.jar
    poi-ooxml.jar
    poi-ooxml-schemas.jar

    二、导出excel常用的组件、样式

     常用组件:
        HSSFWorkbook        excel的文档对象
        HSSFSheet           excel的表单
        HSSFRow             excel的行
        HSSFCell            excel的格子单元
        HSSFFont            excel字体
        HSSFDataFormat      日期格式
        HSSFHeader         sheet头
        HSSFFooter         sheet尾(只有打印的时候才能看到效果)
    
        样式:
        HSSFCellStyle                       cell样式
        辅助操作包括:
        HSSFDateUtil                        日期
        HSSFPrintSetup                      打印
        HSSFErrorConstants                  错误信息表

    我也是借鉴网上的代码我们先上代码

    三、代码实现

    controller

    @RequestMapping(value="/excel")
        public ModelAndView exportExcels() throws Exception{
            ModelAndView mv = new ModelAndView();
            PageData pd = new PageData();
            pd = this.getPageData();
            Session session = Jurisdiction.getSession();
            User user = (User)session.getAttribute(Const.SESSION_USER);         //读取用户信息
            //机关、嘎查
            String organizattion = user.getORGANIZATION_TYPE();
            //党员、党组织
            String type = user.getPARTY_TYPE();
            
            
            //头部信息
            pd.put("ORGANIZATION_TYPE", user.getORGANIZATION_TYPE());
            pd.put("STAR_ORGANIZATION", user.getPARTY_TYPE());
            
                    pd.put("STAR_TYPE", "46e76517e798457fae63a07ee8117c20");
                    List<PageData> generality = starService.findByrating(pd);//共性
                    pd.put("STAR_TYPE", "1434c7d50b15422fa95058a6206ae3fa");
                    List<PageData> duty = starService.findByrating(pd);//职责
                    pd.put("STAR_TYPE", "de17cf5f2f324e3cbe74b4fc15bae381");
                    List<PageData> Optional = starService.findByrating(pd);//自选
            
            
            
                    //list数据
                    pd.put("MPARTY_ID", "4675df71c167458c8da95eb37f13f011");
                    List<PageData> list = relationsService.findAll(pd);//细分查询信息(党委、支部)总表
                    List gen = new ArrayList();
                    List du = new ArrayList();
                    List op = new ArrayList();
                    int n=0;
                    for (PageData pageData : list) {
                        int  number  = (int)Float.parseFloat(pageData.getString("TOTAL"));
                        PageData page = new PageData();
                        for (PageData pad : generality) {//各类星级数据
                            
                        String ID = pad.getString("STAR_ID");
                        
                            String temp1[] = pageData.getString("GENERALITY").split(",");//共性星
                            Arrays.sort(temp1);
                            for (int j = 0; j < temp1.length; j++) {
                                String string = temp1[j];
                                
                                if(ID.equals(string)){
                                    pd.put("STAR_ID", string);
                                    pd.put("USER_ID", pageData.getString("USER_ID"));
                                    PageData STAR_NAME = userstarService.findByStarName(pd);//按星级数据查询总表中的星级数据
                                    if(STAR_NAME.getInt("HAS_STAR")==1){
                                        gen.add("1");
                                        break;
                                    }else if(STAR_NAME.getInt("HAS_STAR")==0){
                                        gen.add("0");
                                        break;
                                    }
                                }else{
                                    gen.add("2");
                                    break;
                                }
                            }
                            n=n+1;
                        }
                        pageData.put("gen", gen);
                        
                        n=0;
                        for (PageData pad : duty) {
                            
                            String ID = pad.getString("STAR_ID");
                            
                                String temp3[] = pageData.getString("DUTY").split(",");//职责星
                                Arrays.sort(temp3);
                                for (int i = 0; i < temp3.length; i++) {
                                    String string = temp3[i];
                                    
                                    if(ID.equals(string)){
                                        pd.put("STAR_ID", string);
                                        pd.put("USER_ID", pageData.getString("USER_ID"));
                                        PageData STAR_NAME = userstarService.findByStarName(pd);
                                        if(STAR_NAME.getInt("HAS_STAR")==1){
                                            du.add("1");
                                            break;
                                        }else if(STAR_NAME.getInt("HAS_STAR")==0){
                                            du.add("0");
                                            break;
                                        }
                                    }else{
                                        du.add("2");
                                        break;
                                    }
                                }
                                n=n+1;
                            }
                        pageData.put("du", du);
                        
                        n=0;
                        for (PageData pad : Optional) {
                            
                                String ID = pad.getString("STAR_ID");
                            
                                String temp2[] = pageData.getString("OPTIONAL").split(",");//自选星
                                Arrays.sort(temp2);
                                for (int i = 0; i < temp2.length; i++) {
                                    String string = temp2[i];
                                    
                                    if(ID.equals(string)){
                                        pd.put("STAR_ID", string);
                                        pd.put("USER_ID", pageData.getString("USER_ID"));
                                        PageData STAR_NAME = userstarService.findByStarName(pd);
                                        if(STAR_NAME.getInt("HAS_STAR")==1){
                                            op.add("1");
                                            break;
                                        }else if(STAR_NAME.getInt("HAS_STAR")==0){
                                            op.add("0");
                                            break;
                                        }
                                    }else{
                                        op.add("2");
                                        break;
                                    }
                                }
                                n=n+1;
                            }
                        pageData.put("op", op);
                        if(number>90){
                            pageData.put("total", "十星级");
                        }else if(number>= 80 && number<= 89){
                            pageData.put("total", "八~九星级");
                        }else if(number>= 70 && number<= 79){
                            pageData.put("total", "六~七星级");
                        }else if(number>= 60 && number<= 69){
                            pageData.put("total", "五星级");
                        }else if(number<59){
                            pageData.put("total", "四星级");
                        }
                    }
            
                    try  
                    {  
                //导出路径 FileOutputStream fout
    = new FileOutputStream("E:/"+ExcelUtils.generateNumber()+".xls"); //导出excel new ExcelUtils().getValue(list, fout, generality.size(), duty.size(), Optional.size(), new ArrayList(), generality, duty, Optional); fout.close(); } catch (Exception e) { e.printStackTrace(); } return mv; }

    try之前都是多表查询统计信息、之后就是调用工具类导出excel

    工具类:ExcelUtil

    package com.fh.util;
    
    
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.List;
    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.HSSFHeader;
    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.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.util.CellRangeAddress;
    
    import java.text.SimpleDateFormat;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Random;
    import java.util.UUID;
    
    import java.util.List;
    
    
    import javax.annotation.Resource;
    
    
    import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
    import me.chanjar.weixin.mp.api.WxMpService;
    import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
    
    
    import org.apache.shiro.session.Session;
    import org.springframework.stereotype.Controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import org.springframework.web.servlet.ModelAndView;
    
    
    
    import com.fh.controller.base.BaseController;
    import com.fh.entity.Page;
    import com.fh.entity.system.User;
    import com.fh.service.car.img.ImgManager;
    import com.fh.service.meeting.meeting.MeetingManager;
    import com.fh.service.meeting.mparty.MpartyManager;
    import com.fh.service.meeting.mtask.MtaskManager;
    import com.fh.service.star.Relations.impl.RelationsService;
    import com.fh.service.star.star.impl.StarService;
    import com.fh.service.star.userstar.impl.UserStarService;
    import com.fh.service.system.dictionaries.impl.DictionariesService;
    import com.fh.service.system.role.impl.RoleService;
    import com.fh.service.system.user.impl.UserService;
    public class ExcelUtils {
        public void getValue(List<PageData> userList,FileOutputStream fout,int n,int m,int k,List<PageData> star,List<PageData> gen,List<PageData> du,List<PageData> op){
              try{
                    //1.创建工作簿
                    HSSFWorkbook workbook = new HSSFWorkbook();
             //设置前4行的表头显示
    //1.1创建合并单元格对象 CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,7+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress1 = new CellRangeAddress(1,1,0,1+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress2 = new CellRangeAddress(1,1,2+n+m+k,7+n+m+k);//起始行,结束行,起始列,结束列 //班组与时间start CellRangeAddress callRangeAddress20 = new CellRangeAddress(2,2,2,n+m+k+1);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress21 = new CellRangeAddress(2,2,n+m+k+2,n+m+k+7);//起始行,结束行,起始列,结束列 //CellRangeAddress callRangeAddress22 = new CellRangeAddress(2,2,5,7);//起始行,结束行,起始列,结束列 //固定列8列 //班组与时间end //标题 CellRangeAddress callRangeAddress31 = new CellRangeAddress(2,4,0,0);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress32 = new CellRangeAddress(2,4,1,1);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress33 = new CellRangeAddress(3,3,2,n+1);//起始行,结束行,起始列,结束列 2 System.out.println(n+"************"+m+"**********"+k); CellRangeAddress callRangeAddress34 = null; if(m>0){ callRangeAddress34 = new CellRangeAddress(3,3,2+n,1+n+m);//起始行,结束行,起始列,结束列 } CellRangeAddress callRangeAddress35 = new CellRangeAddress(3,3,2+n+m,n+m+k+1);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress36 = new CellRangeAddress(3,4,2+n+m+k,2+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress37 = new CellRangeAddress(3,4,3+n+m+k,3+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress38 = new CellRangeAddress(3,4,4+n+m+k,4+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress39 = new CellRangeAddress(3,4,5+n+m+k,5+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress40 = new CellRangeAddress(3,4,6+n+m+k,6+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress41 = new CellRangeAddress(3,4,7+n+m+k,7+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress42 = new CellRangeAddress(userList.size()+8,userList.size()+8,0,7+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress43 = new CellRangeAddress(userList.size()+9,userList.size()+9,0,7+n+m+k);//起始行,结束行,起始列,结束列 CellRangeAddress callRangeAddress44 = new CellRangeAddress(userList.size()+10,userList.size()+10,0,7+n+m+k);//起始行,结束行,起始列,结束列 //设置单元格需要用到的样式 //2.创建工作表 HSSFSheet sheet = workbook.createSheet("派单"); //部项目经理部 HSSFCellStyle headStyle = createCellStyle(workbook,sheet,(short)10,false,true,false,false,null,false,true); //派工单 HSSFCellStyle erStyle = createCellStyle(workbook,sheet,(short)13,true,true,false,false,null,false,false); //班组和时间 HSSFCellStyle sanStyle = createCellStyle(workbook,sheet,(short)10,false,false,false,false,null,false,false); //标题样式 HSSFCellStyle colStyle = createCellStyle(workbook,sheet,(short)10,true,true,false,false,null,false,true); //3级标题样式 //HSSFCellStyle starStyle = createCellStyle(workbook,sheet,(short)10,true,true,true,false,null); //内容样式 HSSFCellStyle cellStyle = createCellStyle(workbook,sheet,(short)10,false,true,false,false,null,false,true); //星星内容样式 HSSFCellStyle stStyle = createCellStyle(workbook,sheet,(short)10,false,true,false,true,null,false,true); //2.1加载合并单元格对象 sheet.addMergedRegion(callRangeAddress); sheet.addMergedRegion(callRangeAddress1); sheet.addMergedRegion(callRangeAddress2); sheet.addMergedRegion(callRangeAddress20); sheet.addMergedRegion(callRangeAddress21); /*sheet.addMergedRegion(callRangeAddress22);*/ sheet.addMergedRegion(callRangeAddress31); sheet.addMergedRegion(callRangeAddress32); sheet.addMergedRegion(callRangeAddress33); if(m>0){ sheet.addMergedRegion(callRangeAddress34); } sheet.addMergedRegion(callRangeAddress35); sheet.addMergedRegion(callRangeAddress36); sheet.addMergedRegion(callRangeAddress37); sheet.addMergedRegion(callRangeAddress38); sheet.addMergedRegion(callRangeAddress39); sheet.addMergedRegion(callRangeAddress40); sheet.addMergedRegion(callRangeAddress41); sheet.addMergedRegion(callRangeAddress42); sheet.addMergedRegion(callRangeAddress43); sheet.addMergedRegion(callRangeAddress44); sheet.setDefaultColumnWidth(15); //3.创建行 //3.1创建头标题行;并且设置头标题 //第一行 HSSFRow rower = sheet.createRow(0); HSSFCell celler = rower.createCell(0); //加载单元格样式 celler.setCellStyle(erStyle); celler.setCellValue("星级管理明细");
             //第二行 HSSFRow rowsan
    = sheet.createRow(1); HSSFCell cellsan = rowsan.createCell(0); HSSFCell cellsan2 = rowsan.createCell(2+n+m+k); //加载单元格样式 cellsan.setCellStyle(sanStyle); cellsan.setCellValue("协作单位:同力科技智能有限公司"); for(int i=1;i<1+n+m+k;i++) { HSSFCell cell2 = rowsan.createCell(i); //加载单元格样式 cell2.setCellStyle(colStyle); cell2.setCellValue(""); } cellsan2.setCellStyle(createCellStyle(workbook,sheet,(short)10,false,true,false,false,null,true,true)); cellsan2.setCellValue(new DataUtil().today()); //3.2创建列标题;并且设置列标题
             //第三行
    HSSFRow row4 = sheet.createRow(2); //遍历各种星的个数插入"" //String[] ti = {"序号","党组织","设星、报星、定星得星情况","","","","","评星、授星情况"};//""为占位字符串 String[] ti = {"序号","党组织"};//""为占位字符串 List<String> tit = new ArrayList(Arrays.asList(ti)); List tit1 = new ArrayList();//添加数据 for (int i = 0; i <= n+m+k-1; i++) { if(i==0){ tit1.add("设星、报星、定星得星情况"); }else{ tit1.add(""); } } List tit2 = new ArrayList(); tit2.add("评星、授星情况"); tit.addAll(tit1); tit.addAll(tit2); String[] titles1 =new String[7+n+m+k]; tit.toArray(titles1); for(int i=0;i<titles1.length;i++) { HSSFCell cell2 = row4.createCell(i); //加载单元格样式 cell2.setCellStyle(colStyle); cell2.setCellValue(titles1[i]); } //3.2创建列标题;并且设置列标题 HSSFRow row2 = sheet.createRow(3); //遍历各种星的个数插入"" //String[] titles = null; //String []arr= {"序号","党组织","共性星","职责星","自选星","年度评议积分","党员评议积分","群众评议积分","加减分","总积分","星级评定结果"};//""为占位字符串 String[] first = {"序号","党组织"}; String[] last = {"年度评议积分","党员评议积分","群众评议积分","加减分","总积分","星级评定结果"}; List<String> list = new ArrayList(Arrays.asList(first)); List gen1 = new ArrayList(); for (int i = 0; i <= n-1; i++) { if(i==0){ gen1.add("共性星"); }else{ gen1.add(""); } } List du1=null; if(m>0){ du1 = new ArrayList(); for (int i = 0; i <= m-1; i++) { if(i==0){ du1.add("职责星"); }else{ du1.add(""); } } } List op1=null; if(k>0){ op1 = new ArrayList(); for (int i = 0; i <= k-1; i++) { if(i==0){ op1.add("自选星"); }else{ op1.add(""); } } } list.addAll(gen1); if(m>0){ list.addAll(du1); } if(k>0){ list.addAll(op1); } list.addAll(Arrays.asList(last)); String[] titles = new String[first.length+last.length+list.size()]; list.toArray(titles); for(int i=0;i<titles.length;i++) { HSSFCell cell2 = row2.createCell(i); //加载单元格样式 cell2.setCellStyle(colStyle); cell2.setCellValue(titles[i]); } HSSFRow rowfour = sheet.createRow(4); //String[] titlefour = {"普工用工数","技工用工数","1","2","3","1","2","3"}; //遍历共性星、职责星、自选星的数据到list中每次都清空 List<String> sta=new ArrayList<String>(); //遍历添加数据 for (PageData pad : gen) { sta.add(pad.getString("STAR_NAME")); } if(m>0){ for (PageData pad : du) { sta.add(pad.getString("STAR_NAME")); } } if(k>0){ for (PageData pad : op) { sta.add(pad.getString("STAR_NAME")); } } //转换
             //排序并给单元格改变宽度、自动换行
    String[] titlefour=new String[n+m+k]; for (int i = 0; i < titlefour.length; i++) { titlefour[i] = sta.get(i); } System.out.println(Arrays.toString(titlefour)); for(int i=0;i<titlefour.length;i++) { HSSFCell cell2 = rowfour.createCell(i+2); //加载单元格样式星指标 cell2.setCellStyle(createCellStyle(workbook,sheet,(short)10,true,true,true,false,i+2,true,true)); cell2.setCellValue(titlefour[i]); } //4.操作单元格;将用户列表写入excel
             //
    将星星和空数据插入到Excel中 if(userList != null) { int i=1; for(PageData pag : userList) { //创建数据行,前面有两行,头标题行和列标题行 HSSFRow row3 = sheet.createRow(i+4); HSSFCell cell0 = row3.createCell(0); cell0.setCellStyle(cellStyle); cell0.setCellValue(i++); HSSFCell cell1 = row3.createCell(1); cell1.setCellStyle(cellStyle); cell1.setCellValue(pag.getString("PARTY_NAME")); List<String> lisMap = new ArrayList<String>(); lisMap=(List<String>) pag.get("gen"); int f=0; for (int l = 2; l < n+2; l++) { String string = lisMap.get(f); HSSFCell cell2 = row3.createCell(l); if(string=="2"){ cell2.setCellStyle(cellStyle); cell2.setCellValue(""); }else if(string =="1"){ cell2.setCellStyle(stStyle); cell2.setCellValue("★"); }else if(string == "0"){ cell2.setCellStyle(cellStyle); cell2.setCellValue("☆"); } f++; } if(m>0){ f=0; List<String> lisMap1 = new ArrayList<String>(); lisMap1=(List<String>) pag.get("du"); for (int l = 2+n; l < 2+n+m; l++) { String string = lisMap1.get(f); HSSFCell cell2 = row3.createCell(l); if(string=="2"){ cell2.setCellStyle(cellStyle); cell2.setCellValue(""); }else if(string =="1"){ cell2.setCellStyle(stStyle); cell2.setCellValue("★"); }else if(string == "0"){ cell2.setCellStyle(cellStyle); cell2.setCellValue("☆"); } f++; } } if(k>0){ f=0; List<String> lisMap2 = new ArrayList<String>(); lisMap2=(List<String>) pag.get("op"); for (int l = 2+n+m; l < n+m+k+2; l++) { String string = lisMap2.get(f); HSSFCell cell2 = row3.createCell(l); if(string=="2"){ cell2.setCellStyle(cellStyle); cell2.setCellValue(""); }else if(string =="1"){ cell2.setCellStyle(stStyle); cell2.setCellValue("★"); }else if(string == "0"){ cell2.setCellStyle(cellStyle); cell2.setCellValue("☆"); } f++; } } HSSFCell cell4 = row3.createCell(2+n+m+k); cell4.setCellStyle(cellStyle); cell4.setCellValue(pag.getString("YEAR_EVAL")); HSSFCell cell5 = row3.createCell(3+n+m+k); cell5.setCellStyle(cellStyle); cell5.setCellValue(pag.getString("DY_EVAL")); HSSFCell cell6 = row3.createCell(4+n+m+k); cell6.setCellStyle(cellStyle); cell6.setCellValue(pag.getString("QZ_EVAL")); HSSFCell cell7 = row3.createCell(5+n+m+k); cell7.setCellStyle(cellStyle); cell7.setCellValue(pag.getString("OTHER_POINTS")); HSSFCell cell8 = row3.createCell(6+n+m+k); cell8.setCellStyle(cellStyle); cell8.setCellValue(pag.getString("TOTAL")); HSSFCell cell9 = row3.createCell(7+n+m+k); cell9.setCellStyle(cellStyle); cell9.setCellValue(pag.getString("total")); } } HSSFRow rownumber = sheet.createRow(userList.size()+8); HSSFCell cellnumber = rownumber.createCell(0); HSSFCell cellnumber1 = rownumber.createCell(3); //加载单元格样式 cellnumber.setCellStyle(sanStyle); cellnumber.setCellValue("备注:1.‘★’代表当住址设星、报星,并得星;‘☆’代表党组织设星、报星,但未得星的。"); HSSFRow rowpersion = sheet.createRow(userList.size()+9); HSSFCell cellpersion = rowpersion.createCell(0); //加载单元格样式 cellpersion.setCellStyle(sanStyle); cellpersion.setCellValue(" 2.按评星高低排序填写,‘十星级’党组织比例原则上不超过20%,"); HSSFRow rowinfo = sheet.createRow(userList.size()+10); HSSFCell cellinfo = rowinfo.createCell(0); cellinfo.setCellStyle(sanStyle); cellinfo.setCellValue(" ‘四星级’及以下当住址比例原则上不低于5%。"); HSSFRow rowinfo1 = sheet.createRow(userList.size()+10); HSSFCell cellinfo1 = rowinfo1.createCell(0); cellinfo1.setCellStyle(sanStyle); HSSFRow rowinfo2 = sheet.createRow(userList.size()+11); HSSFCell cellinfo2 = rowinfo2.createCell(0); cellinfo2.setCellStyle(sanStyle); //5.输出 workbook.write(fout); // workbook.close(); //out.close(); }catch(Exception e) { e.printStackTrace(); } } /** * * @param workbook * @param fontsize * @return 单元格样式
       * 根据参数来改变单元格样式
    */ private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook,HSSFSheet sheet, short fontsize,boolean flag,boolean flag1,boolean direction,boolean color,Integer number,boolean align,boolean frame) { // TODO Auto-generated method stub HSSFCellStyle style = workbook.createCellStyle(); HSSFSheet sheet1 = sheet; //是否水平居中 if(flag1){ style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 //style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//右对齐 } //时间对齐方式 if(align){ style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//右对齐 } //边框 if(frame){ /* style.setBorderBottom(CellStyle.BORDER_THIN); // 下边框 style.setBorderLeft(CellStyle.BORDER_THIN);// 左边框 style.setBorderTop(CellStyle.BORDER_THIN);// 上边框 style.setBorderRight(CellStyle.BORDER_THIN);// 右边框 */ } style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //创建字体 HSSFFont font = workbook.createFont(); //是否加粗字体 if(flag){ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } //字体颜色 if(color){ font.setColor(HSSFColor.RED.index); }else{ font.setColor(HSSFColor.BLACK.index); } //文字方向 if(direction){ //style.setDataFormat(fmt) style.setWrapText(true); sheet.setColumnWidth(number, 3*256); }
        //设置字体高度 font.setFontHeightInPoints(fontsize);
    //加载字体 style.setFont(font); return style; }

      //获取一个8位数的随机数
    public static String generateNumber() { String no=""; int num[]=new int[8]; int c=0; for (int i = 0; i < 8; i++) { num[i] = new Random().nextInt(10); c = num[i]; for (int j = 0; j < i; j++) { if (num[j] == c) { i--; break; } } } if (num.length>0) { for (int i = 0; i < num.length; i++) { no+=num[i]; } } return no; } }

     其实最主要的是Excel合并单元格,你合并来多少个单元格你就得有多少个空字符(例如:上面定星、设星、报星情况加上自己的有合并12个必须自己的文字加11个空“”组成的数组给赋值到这行当中)

    另外就是Excel中还有个坑是合并的单元格他有时候不会给你加上边框线

    还有就是合并单元格多的时候一定要细心不然老是搞错就烦的很。

    如果有哪里感到不足请在评论区指出下,让我也借鉴一下

  • 相关阅读:
    Web API初印象
    SQL注入总结篇
    Debian Linux 下安装pip3
    DVWA:环境搭建
    AWVS使用手册
    常见的反爬虫和应对方法
    Fiddler抓取手机APP数据包
    爬虫 Scrapy框架 爬取图虫图片并下载
    python 爬虫基础知识(继续补充)
    Python 爬虫 多进程清洗代理
  • 原文地址:https://www.cnblogs.com/wolf-shuai/p/13628382.html
Copyright © 2011-2022 走看看