• java 导出excel(读数据库案例)


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    public class SheetDataSource {
    public static Connection getConn() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");
    return DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "*****", "******");
    }
    public static ResultSet selectAllDataFromDB() throws Exception{
    Connection conn = getConn();
    Statement sm = conn.createStatement();
    return sm.executeQuery("select * from user");
    }
    }

    ============================================================

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.*;
    import javax.swing.JOptionPane;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFFooter;
    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.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.Color;
    import org.apache.poi.ss.usermodel.Font;

    public class ExcelService {
    // 表头
    public static final String[] tableHeader = {"id","username","age"};
    // 创建工作本
    public static HSSFWorkbook demoWorkBook = new HSSFWorkbook();
    // 创建表
    public static HSSFSheet demoSheet = demoWorkBook.createSheet("用户信息");
    // 表头的单元格个数目
    public static final short cellNumber = (short)tableHeader.length;
    // 数据库表的列数
    public static final int columNumber = cellNumber; //3
    /**
    * 34.* 创建表头 35.*
    *
    * @return 36.
    */
    @SuppressWarnings("deprecation")
    public static void createTableHeader()
    {
    //设置表头,从sheet中得到
    HSSFHeader header = demoSheet.getHeader();
    header.setCenter("用户表");
    //创建一行
    HSSFRow headerRow = demoSheet.createRow((short) 0);
    for(int i = 0;i < cellNumber;i++)
    {
    //创建一个单元格
    HSSFCell headerCell = headerRow.createCell((short) i);
    // headerCell.setEncoding(HSSFCell.ENCODING_UTF_16);
    // CellStyle cs = new CellStyle();
    //设置cell的值
    System.out.println("tableHeader[i]=="+tableHeader[i]);
    headerCell.setCellValue(tableHeader[i]);
    }
    }
    /**
    * 50.* 创建行 51.*
    *
    * @param cells
    * 52.*
    * @param rowIndex
    */
    @SuppressWarnings("deprecation")
    public static void createTableRow(List<String> cells , short rowIndex)
    {
    // 创建第rowIndex行
    HSSFRow row = demoSheet.createRow((short) rowIndex);
    for(short i = 0;i < cells.size();i++)
    {
    // 创建第i个单元格
    HSSFCell cell = row.createCell((short) i);
    //cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    cell.setCellValue(cells.get(i));
    }
    }
    /**
    * 68.* 创建整个Excel表 69.*
    *
    * @throws SQLException
    * 70.* 71.
    */
    public static void createExcelSheeet() throws Exception
    {
    createTableHeader(); //--->创建一个表头行
    ResultSet rs = SheetDataSource.selectAllDataFromDB(); //--->得到所有数据
    int rowIndex = 1;
    while(rs.next())
    {
    List<String> list = new ArrayList<String>();
    for(int i = 1;i <= columNumber;i++)
    {
    list.add(rs.getString(i));
    }
    createTableRow(list,(short)rowIndex);
    rowIndex++;
    }
    }
    /**
    * 89.* 导出表格 90.*
    *
    * @param sheet
    * 91.*
    * @param os
    * 92.*
    * @throws IOException
    * 93.
    */
    public void exportExcel(HSSFSheet sheet,OutputStream os) throws IOException
    {
    sheet.setGridsPrinted(true);
    HSSFFooter footer = sheet.getFooter();
    footer.setRight("Page " + HSSFFooter.page() + " of " +
    HSSFFooter.numPages());
    System.out.println("HSSFFooter.page()="+HSSFFooter.page()+" "+"HSSFFooter.numPages()="+HSSFFooter.numPages());
    demoWorkBook.write(os);
    }
    public static void main(String[] args) {
    String fileName = "D:\用户信息.xls";
    FileOutputStream fos = null;
    try {
    ExcelService pd = new ExcelService();
    ExcelService.createExcelSheeet();
    fos = new FileOutputStream(fileName);
    pd.exportExcel(demoSheet,fos);
    JOptionPane.showMessageDialog(null, "表格已成功导出到 : "+fileName);
    } catch (Exception e) {
    JOptionPane.showMessageDialog(null, "表格导出出错,错误信息 :"+e+" 错误原因可能是表格已经打开。");
    e.printStackTrace();
    } finally {
    try {
    fos.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }

    ======================================================

    用到的jar包

    poi-3.8.jar
    poi-examples-3.8.jar
    poi-excelant-3.8.jar
    poi-ooxml-3.8.jar
    poi-ooxml-schemas-3.8.jar
    poi-scratchpad-3.8.jar

  • 相关阅读:
    自然语言处理1-1 算法时间复杂度
    tensorflow2.0(1):简介
    基于PKCS#11的应用架构
    数字证书应用改造需求调研-2015-10-19
    签名验证工具Aloaha Sign
    PDF签名应用研究小结
    用bcp导sql server数据到excel文件
    无线网卡和有线网卡的嗅探模式的区别
    Tomcat6源代码在Eclipse里调试简要说明
    OpenLDAP 主从复制配置
  • 原文地址:https://www.cnblogs.com/austinspark-jessylu/p/6010770.html
Copyright © 2020-2023  润新知