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中還有個坑是合並的單元格他有時候不會給你加上邊框線

還有就是合並單元格多的時候一定要細心不然老是搞錯就煩的很。

如果有哪里感到不足請在評論區指出下,讓我也借鑒一下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM