用jxl将mysql的数据结构导入到excel中,用过poi将oracle表结构导入

效果图如下:


package com.example.test;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.format.BorderLineStyle;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Boolean;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class HelperTest3 {
    static WritableWorkbook wwb = null;
    static OutputStream os = null;
    private int row=0;
    static{
        String filePath = "D:\\excel\\Test.xls";
        File file = new File(filePath);
        try {
            if(!file.isFile()){//如果指定文件不存在,则新建该文件
                file.createNewFile();
            }
            os = new FileOutputStream(file);//创建一个输出流
            wwb = Workbook.createWorkbook(os);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    static List<String> listName=new ArrayList<String>();
    static List<String> listType=new ArrayList<String>();
    static List<String> listLength=new ArrayList<String>();
    static List<String> listNum=new ArrayList<String>();
    static List list=new ArrayList();

    public static void main(String[] args) {
        //序号    列名    数据类型    长度    小数位    标识    主键    外键    允许空    默认值    说明
        String user="root";
        String password="";
        String url="jdbc:mysql://localhost:3306/user";
        String driver="com.mysql.jdbc.Driver";
        String tableName="Book";
        String sqlstr;
        Connection con=null;
        Statement stmt=null;
        ResultSet rs=null;
        boolean isLast=false;


        try {
            Class.forName(driver);
            con=DriverManager.getConnection(url,user,password);
            stmt=con.createStatement();
            list = new HelperTest3().getAllTableName(con);


            listName.add("列名");
            listType.add("字段类型");
            listLength.add("长度");
            listNum.add("序号");

            for (int iTable = 0; iTable < list.size(); iTable++) {
                tableName=(String) list.get(iTable);

                sqlstr="select * from "+tableName;
                rs=stmt.executeQuery(sqlstr);

                ResultSetMetaData rsd=rs.getMetaData();
                for(int i = 0; i < rsd.getColumnCount(); i++) {  
                    listName.add(rsd.getColumnName(i + 1));
                    listType.add(rsd.getColumnTypeName(i + 1));
                    listLength.add (String.valueOf(rsd.getColumnDisplaySize(i + 1)));
                    listNum.add (String.valueOf(i+1));


                    System.out.print("列名:"+rsd.getColumnName(i + 1));  
                    System.out.print("字段类型:"+rsd.getColumnTypeName(i + 1));  
                    System.out.print("长度:"+rsd.getColumnDisplaySize(i + 1));  
                    System.out.println();  
                }  
                System.out.println("tableName:"+tableName);
                System.out.println(listName.toString());
                System.out.println(listType.toString());
                System.out.println(listLength.toString());
                System.out.println(listNum.toString());

                isLast=(iTable==list.size()-1?true:false);
                new HelperTest3().addToExcel(listName, listType, listLength,listNum,tableName,iTable,isLast);

                listName.clear();
                listType.clear();
                listLength.clear();
                listNum.clear();

                listName.add("列名");
                listType.add("字段类型");
                listLength.add("长度");
                listNum.add("序号");
            }
            
            try {
                System.out.println("成功");
                //            System.out.println("sheet.getRows()"+sheet.getRows());
                wwb.write();//将内容写到excel文件中
                os.flush();//清空输出流
                
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(wwb != null)
                        wwb.close();
                    if(os != null)
                        os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public  void addToExcel(List<String> listName,List<String> listType,
            List<String> listLength,List<String> listNum,String tableName,int k, boolean isLast){
        WritableSheet sheet = null;
        try {
            //            sheet = wwb.createSheet("sheet1", 0);//创建一个工作页,第一个参数的页名,第二个参数表示该工作页在excel中处于哪一页
            //创建sheet之后就不能找到以前的sheet?
            //            sheet = wwb.createSheet("sheet1", 0);//创建一个工作页,第一个参数的页名,第二个参数表示该工作页在excel中处于哪一页

            //            sheet = wwb.createSheet("sheet2", 0);//创建一个工作页,第一个参数的页名,第二个参数表示该工作页在excel中处于哪一页

            if (k==0) {
                sheet = wwb.createSheet("sheet1", 0);
            }else {
                sheet=wwb.getSheet(0);
            }



            /*String[] sNames = wwb.getSheetNames();
            for (int i = 0; i < sNames.length; i++) {
                System.out.println("///////");
                System.out.println(sNames[i]+"\n");
            }*/


            row=sheet.getRows();
            creatBasicData(listName, listType, listLength, listNum, tableName,
                    sheet,row);
            //生成第一栏
            creatTop(sheet,row);
            //正常style

            creatNomorlStyle(listName, sheet,row);

            System.out.println("sheet.rows"+sheet.getRows());


            
        } catch (WriteException e) {
            e.printStackTrace();
        } finally {
        }

    }
    private void creatBasicData(List<String> listName, List<String> listType,
            List<String> listLength, List<String> listNum, String tableName,
            WritableSheet sheet, int row) throws WriteException, RowsExceededException {
        System.out.println("此时的row为"+row);
        //第一个参数表示列,第二个参数表示行
        Label label = new Label(0,0+row,tableName);//填充第一行第一个单元格的内容
        sheet.addCell(label);

        for (int i = 0; i < listName.size(); i++) {
            //i+1行是为了让出第一行的表名
            WritableCellFormat wcfFC = testStyle(i);

            label = new Label(0,i+1+row,listNum.get(i),wcfFC);//定制单元格格式,//填充第二列,以此类推

            sheet.addCell(label);

            label = new Label(1,i+1+row,listName.get(i),wcfFC);
            sheet.addCell(label);

            label = new Label(2,i+1+row,listType.get(i),wcfFC);
            sheet.addCell(label);

            label = new Label(3,i+1+row,listLength.get(i),wcfFC);
            sheet.addCell(label);


        }
    }

    private void creatNomorlStyle(List<String> listName, WritableSheet sheet, int row)
            throws WriteException, RowsExceededException {
        Label label;
        List<String>list=new ArrayList<String>();
        for (int i = 0; i < listName.size()-1; i++) {
            list.add(i==0?"否":"是");
        }
        WritableCellFormat wcfFC = testStyle(1);
        for (int i = 4; i <= 10; i++) {
            for (int j = 2; j <= listName.size(); j++) {
                if (i==8) {
                    label = new Label(i,j+row,list.get(j-2),wcfFC);//填充第8列的元素i=8
                }else {
                    if (i==6&&j==2) {
                        label = new Label(i,j+row,"是",wcfFC);//填充第一行第一个单元格的内容
                    }else {
                        label = new Label(i,j+row,"",wcfFC);//填充第一行第一个单元格的内容
                    }
                }

                sheet.addCell(label);
            }
        }
    }

    private void creatTop(WritableSheet sheet, int row) throws WriteException,
    RowsExceededException {
        Label label;
        //小数位    标识    主键    外键    允许空     默认值      说明
        //4     5    6   7   8    9    10
        List<String>list=new ArrayList<String>();
        list.add("小数位");
        list.add("标识");
        list.add("主键");
        list.add("外键");
        list.add("允许空");
        list.add("默认值");
        list.add("说明");

        //粗体加背景
        WritableCellFormat wcfFC = testStyle(0);
        for (int i = 0; i < list.size(); i++) {
            label = new Label(i+4,1+row,list.get(i),wcfFC);//填充第一行第一个单元格的内容
            sheet.addCell(label);
        }
    }

    private WritableCellFormat testStyle(int i) throws WriteException {
        WritableFont wfc=null;
        if (i==0) {
            wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,
                    UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);

        }else {
            wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,
                    UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
        }
        WritableCellFormat wcfFC = new WritableCellFormat(wfc);

        //设置边框为实线
        wcfFC.setAlignment(jxl.write.Alignment.CENTRE);
        //设置垂直对齐为居中对齐
        wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
        //设置顶部边框线为实线(默认是黑色--也可以设置其他颜色)
        wcfFC.setBorder(jxl.format.Border.TOP, BorderLineStyle.THIN);
        //设置右边框线为实线
        wcfFC.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.THIN);
        //设置底部边框线为实线
        wcfFC.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.THIN);
        //设置左边框线为实线
        wcfFC.setBorder(jxl.format.Border.LEFT, BorderLineStyle.THIN);

        if (i==0) {
            wcfFC.setBackground(jxl.format.Colour.GRAY_25);//设置单元格的颜色为浅灰色
        }
        return wcfFC;
    }


    public  List   getAllTableName(Connection   cnn)   throws   SQLException{    
        List   tables   =   new   ArrayList();    

        DatabaseMetaData   dbMetaData   =   cnn.getMetaData();    

        //可为:"TABLE",   "VIEW",   "SYSTEM   TABLE",      
        //"GLOBAL   TEMPORARY",   "LOCAL   TEMPORARY",   "ALIAS",   "SYNONYM"    
        String[]   types   =   {"TABLE"};    

        ResultSet   tabs   =   dbMetaData.getTables(null,   null,   null,types/*只要表就好了*/);    
        /*记录集的结构如下:   
            TABLE_CAT       String   =>   table   catalog   (may   be   null)     
            TABLE_SCHEM   String   =>   table   schema   (may   be   null)     
            TABLE_NAME     String   =>   table   name     
            TABLE_TYPE     String   =>   table   type.     
            REMARKS           String   =>   explanatory   comment   on   the   table     
            TYPE_CAT         String   =>   the   types   catalog   (may   be   null)     
            TYPE_SCHEM     String   =>   the   types   schema   (may   be   null)     
            TYPE_NAME       String   =>   type   name   (may   be   null)     
            SELF_REFERENCING_COL_NAME   String   =>   name   of   the   designated   "identifier"   column   of   a   typed   table   (may   be   null)     
            REF_GENERATION   String   =>   specifies   how   values   in   SELF_REFERENCING_COL_NAME   are   created.   Values   are   "SYSTEM",   "USER",   "DERIVED".   (may   be   null)     
         */    
        while(tabs.next()){    
            //只要表名这一列    
            tables.add(tabs.getObject("TABLE_NAME"));    

        }    
        System.out.println(tables);    
        return   tables;    

    }  

}

oracle导入如下:其中俩MK是联合主键


三个类:

-----------------------------------------------------------------------------------------------

package com.test.testOracleExcel;


import java.io.Serializable;


/**


 */
public class AllYouNeedBean implements Serializable{
    private String TABLE_NAME;
    private String COLUMN_NAME;
    private String DATA_TYPE;
    private String DATA_LENGTH;
    private String DATA_SCALE;
    private String NULLABLE;
    private String DATA_DEFAULT;
    private String COMMENTS;
    private String U_KEY;


    public AllYouNeedBean() {
    }


    public String getCOLUMN_NAME() {
        return COLUMN_NAME;
    }


    public void setCOLUMN_NAME(String COLUMN_NAME) {
        this.COLUMN_NAME = COLUMN_NAME;
    }


    public String getDATA_TYPE() {
        return DATA_TYPE;
    }


    public void setDATA_TYPE(String DATA_TYPE) {
        this.DATA_TYPE = DATA_TYPE;
    }


    public String getDATA_LENGTH() {
        return DATA_LENGTH;
    }


    public void setDATA_LENGTH(String DATA_LENGTH) {
        this.DATA_LENGTH = DATA_LENGTH;
    }


    public String getDATA_SCALE() {
        return DATA_SCALE;
    }


    public void setDATA_SCALE(String DATA_SCALE) {
        this.DATA_SCALE = DATA_SCALE;
    }


    public String getNULLABLE() {
        return NULLABLE;
    }


    public void setNULLABLE(String NULLABLE) {
        this.NULLABLE = NULLABLE;
    }


    public String getDATA_DEFAULT() {
        return DATA_DEFAULT;
    }


    public void setDATA_DEFAULT(String DATA_DEFAULT) {
        this.DATA_DEFAULT = DATA_DEFAULT;
    }


    public String getCOMMENTS() {
        return COMMENTS;
    }


    public void setCOMMENTS(String COMMENTS) {
        this.COMMENTS = COMMENTS;
    }


    public String getU_KEY() {
        return U_KEY;
    }


    public void setU_KEY(String u_KEY) {
        U_KEY = u_KEY;
    }


    public String getTABLE_NAME() {
        return TABLE_NAME;
    }


    public void setTABLE_NAME(String TABLE_NAME) {
        this.TABLE_NAME = TABLE_NAME;
    }


    @Override
    public String toString() {
        return "AllYouNeedBean{" +
                "TABLE_NAME='" + TABLE_NAME + '\'' +
                ", COLUMN_NAME='" + COLUMN_NAME + '\'' +
                ", DATA_TYPE='" + DATA_TYPE + '\'' +
                ", DATA_LENGTH='" + DATA_LENGTH + '\'' +
                ", DATA_SCALE='" + DATA_SCALE + '\'' +
                ", NULLABLE='" + NULLABLE + '\'' +
                ", DATA_DEFAULT='" + DATA_DEFAULT + '\'' +
                ", COMMENTS='" + COMMENTS + '\'' +
                ", U_KEY='" + U_KEY + '\'' +
                '}';
    }
}

------------------------------------------------------------------------------------------

package com.test.testOracleExcel;


import jxl.Workbook;
import jxl.write.WritableWorkbook;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;


import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;


/**


 */
public class HelperTest5 {
    static JdbcTemplate jdbcTemplate = null;


    static {
        try {
            String user = "自己改啊各位";
            String password = "自己改啊各位";
            String url = "jdbc:oracle:thin:@自己改啊各位";
            //String driver="com.mysql.jdbc.Driver";
            String driver = "oracle.jdbc.driver.OracleDriver";


            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(user);
            dataSource.setPassword(password);


            jdbcTemplate = new JdbcTemplate(dataSource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static BeanPropertyRowMapper<AllYouNeedBean> tableNameRowMapper = new BeanPropertyRowMapper<AllYouNeedBean>() {
        @Override
        public AllYouNeedBean mapRow(ResultSet rs, int rowNumber) throws SQLException {
            AllYouNeedBean bean = null;
            bean = new AllYouNeedBean();
            bean.setTABLE_NAME(rs.getString("TABLE_NAME"));
            bean.setCOLUMN_NAME(rs.getString("COLUMN_NAME"));
            bean.setCOMMENTS(rs.getString("COMMENTS"));
            bean.setDATA_LENGTH(rs.getString("DATA_LENGTH"));
            bean.setDATA_TYPE(rs.getString("DATA_TYPE"));
            bean.setNULLABLE(rs.getString("NULLABLE"));
            bean.setDATA_SCALE(rs.getString("DATA_SCALE"));
            bean.setU_KEY(rs.getString("U_KEY"));


            byte[] arr =null;


           /* Reader is1 = rs.getNCharacterStream("DATA_DEFAULT");
            InputStream stream = rs.getBinaryStream("DATA_DEFAULT");
            try {
                if (stream != null) {
                    System.out.println("------------------not null--------------------");
                    arr = readStream(stream);
                    bean.setDATA_DEFAULT("" + new String(arr));
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            bean.setDATA_DEFAULT("");*/
            return bean;
        }
    };


    public static byte[] readStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return outSteam.toByteArray();
    }




    public static void main(String[] args) {
        System.out.println("-------------------------" +
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "调sql前--------------------------------");
        List<AllYouNeedBean> list = getList();
        System.out.println("-------------------------" +
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "sql调完--------------------------------");
        WriteToExcelUtil.exporteExcel(list);
        System.out.println("-------------------------" +
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "excel导完--------------------------------");
    }


    public static List<AllYouNeedBean> getList() {
        String sql = "select *" +
                "  from (select t3.*, t4.u_key as u_key " +
                "          from (select t1.table_name, " +
                "                       t1.column_name," +
                "                       t2.DATA_TYPE," +
                "                       t2.DATA_LENGTH," +
                "                       t2.DATA_SCALE," +
                "                       t2.NULLABLE," +
                "                       t1.comments, " +
                "                       t2.DATA_DEFAULT" +
                "                  from user_col_comments t1 " +
                "                  left join user_tab_columns t2 on t1.table_name =" +
                "                                                   t2.TABLE_NAME " +
                "                                               and t1.column_name =" +
                "                                                   t2.COLUMN_NAME) t3 " +
                "          left join (select au.table_name, cu.column_name, 'MK' as u_key " +
                "                      from user_cons_columns cu, user_constraints au " +
                "                     where cu.constraint_name = au.constraint_name " +
                "                       and au.constraint_type = 'P'" +
                "                       and au.table_name in " +
                "                           (select table_name from user_tables)" +
                "                    union all " +
                "                    select b.table_name, b.column_name, 'FK' as u_key " +
                "                      from user_cons_columns b " +
                "                     where b.constraint_name =" +
                "                           (select c.constraint_name " +
                "                              from user_constraints c " +
                "                             where c.constraint_type = 'R'" +
                "                               and c.table_name in " +
                "                                   (select table_name from user_tables))) t4 on t3.table_name =" +
                "                                                                                t4.table_name " +
                "                                                                            and t3.column_name =" +
                "                                                                                t4.column_name)";
        List<AllYouNeedBean> list = jdbcTemplate.query(sql, tableNameRowMapper);


        return list;
    }


}

----------------------------------------------------------------------------------------------------

package com.test.testOracleExcel;


import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;


import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 
 */
public class WriteToExcelUtil {


    static OutputStream os = null;


    static int rowNum = -1; //  -1方便首次增加
    //序号    列名    数据类型    长度    小数位       主键    外键    允许空    默认值    注释
    static String[] headers = {"序号", "名字", "类型", "长度", "小数位", "主键", "外键", "非空", "默认值", "注释"};


    /**
     * 初始化流
     */
    static {
        String filePath = "D:\\excel\\Test1.xls";
        File file = new File(filePath);
        try {
            if (!file.isFile()) {//如果指定文件不存在,则新建该文件
                file.createNewFile();
            }
            os = new FileOutputStream(file);//创建一个输出流
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void exporteExcel(List<AllYouNeedBean> list) {
        try {
            HSSFWorkbook workBook = new HSSFWorkbook();
            HSSFSheet sheet = workBook.createSheet("heheTitle");
            //sheet.setDefaultColumnWidth(15);
            //设置表名,表头,普通单元格的格式
            HSSFCellStyle headStyle = setHeaderStyle(workBook);
            HSSFCellStyle bodyStyle = setBodyStyle(workBook);
            HSSFCellStyle tableNameStyle = setTableNameStyle(workBook);


            String table_name = "heheheTable";//只是做首行判断
            int numberId = 0;
            for (AllYouNeedBean bean : list) {
                if (bean.getTABLE_NAME().equals(table_name)) {
                    //其余行就判断是否跟以前一样
                    rowNum++;
                    numberId++;
                    setNormallCellValue(sheet, bodyStyle, numberId, bean);
                } else {
                    //第一行就新建table_name和header
                    //并新建第一行数据
                    //rowNum增加了3
                    table_name = bean.getTABLE_NAME();
                    createTableNameAndHeader(sheet, headStyle, tableNameStyle, table_name);
                    rowNum++;
                    numberId = 1;
                    setNormallCellValue(sheet, bodyStyle, numberId, bean);
                }
            }
            writeAndCloseStream(workBook);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void writeAndCloseStream(HSSFWorkbook workBook) {
        try {
            workBook.write(os);
            os.flush();//清空输出流
        } catch (IOException e) {
            System.out.println("写入文件失败" + e.getMessage());
        } finally {
            try {
                if (os != null)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private static void createTableNameAndHeader(HSSFSheet sheet, HSSFCellStyle headStyle, HSSFCellStyle tableNameStyle, String table_name) {
        //创建表名
        rowNum++;
        HSSFRow rowTitle = sheet.createRow(rowNum);
        HSSFCell cellTitle = rowTitle.createCell(0);
        cellTitle.setCellStyle(tableNameStyle);
        cellTitle.setCellValue(table_name);
        //创建header列
        rowNum++;
        HSSFRow row = sheet.createRow(rowNum);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(headStyle);
            HSSFRichTextString textString = new HSSFRichTextString(headers[i]);
            cell.setCellValue(textString);
            //sheet.autoSizeColumn((short) i);//设置内容自适应大小
        }
    }


    private static void setNormallCellValue(HSSFSheet sheet, HSSFCellStyle bodyStyle, int index, AllYouNeedBean bean) {
        HSSFRow row = sheet.createRow(rowNum);
        for (int j = 0; j < headers.length; j++) {
            HSSFCell cell = row.createCell(j);
            cell.setCellStyle(bodyStyle);
            HSSFRichTextString textString = null;
            switch (j) {
                case 0:
                    textString = new HSSFRichTextString(String.valueOf(index));
                    break;
                case 1:
                    textString = new HSSFRichTextString(bean.getCOLUMN_NAME());
                    break;
                case 2:
                    textString = new HSSFRichTextString(bean.getDATA_TYPE());
                    break;
                case 3:
                    textString = new HSSFRichTextString(bean.getDATA_LENGTH());
                    break;
                case 4:
                    textString = new HSSFRichTextString(bean.getDATA_SCALE());
                    break;
                case 5:
                    textString = new HSSFRichTextString(StringUtils.isEmpty(bean.getU_KEY()) ? "" : bean.getU_KEY().equals("MK") ? "MK" : "");
                    break;
                case 6:
                    textString = new HSSFRichTextString(StringUtils.isEmpty(bean.getU_KEY()) ? "" : bean.getU_KEY().equals("FK") ? "FK" : "");
                    break;
                case 7:
                    textString = new HSSFRichTextString(bean.getNULLABLE());
                    break;
                case 8:
                    textString = new HSSFRichTextString(bean.getDATA_DEFAULT());
                    break;
                case 9:
                    textString = new HSSFRichTextString(bean.getCOMMENTS());
                    break;
                default:
                    textString = new HSSFRichTextString("空值啊");
                    break;
            }
            cell.setCellValue(textString);
        }
    }


    /**
     * 设置正文单元格格式
     *
     * @param workBook
     * @return
     */
    private static HSSFCellStyle setBodyStyle(HSSFWorkbook workBook) {
        HSSFCellStyle style2 = workBook.createCellStyle();
        style2.setFillForegroundColor(HSSFColor.WHITE.index);
        style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);


        HSSFFont font2 = workBook.createFont();
        font2.setFontName("微软雅黑");
        font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        style2.setFont(font2);
        return style2;
    }


    /**
     * 设置表头格式
     *
     * @param workBook
     * @return
     */
    private static HSSFCellStyle setHeaderStyle(HSSFWorkbook workBook) {
        HSSFCellStyle style = workBook.createCellStyle();
        style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBottomBorderColor(HSSFColor.LIGHT_YELLOW.index);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);


        HSSFFont font = workBook.createFont();
        font.setFontName("微软雅黑");
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);
        return style;
    }


    /**
     * 设置表头格式
     *
     * @param workBook
     * @return
     */
    private static HSSFCellStyle setTableNameStyle(HSSFWorkbook workBook) {
        HSSFCellStyle style = workBook.createCellStyle();
        style.setFillForegroundColor(HSSFColor.CORNFLOWER_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBottomBorderColor(HSSFColor.CORNFLOWER_BLUE.index);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        style.setFillBackgroundColor(HSSFColor.SKY_BLUE.index);


        HSSFFont font = workBook.createFont();
        font.setFontName("微软雅黑");
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);
        return style;
    }




}



可以导出数据结构,就是data-default还是有点问题,数据库中默认存储的是long结构,不能用正常的方式取出,

估计还得写存储结构,这个我在研究一下,再编辑吧


猜你喜欢

转载自blog.csdn.net/xiaoe3504xiaoe/article/details/53761153
今日推荐