Home > Database > Mysql Tutorial > body text

sqlserver数据写入到excel

WBOY
Release: 2016-06-07 15:51:19
Original
1229 people have browsed it

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入 使用 c3p0获取 DataSource public class DataSourceUtils { private static DataSource ds; static{ //默认的读取c3p0-config.xml中默认配置 ds = new ComboPooledDataSource(); } public stati

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入

  使用 c3p0获取 DataSource

  public class DataSourceUtils {

  private static DataSource ds;

  static{

  //默认的读取c3p0-config.xml中默认配置

  ds = new ComboPooledDataSource();

  }

  public static DataSource getDatasSource(){

  return ds;

  }

  public static Connection getConn(){

  Connection con = null;

  try{

  con = ds.getConnection();//每一次从ds中获取一个新的连接

  }catch(Exception e){

  e.printStackTrace();

  }

  return con;

  }

  }

  使用 poi 将数据写入到excel中

  public void createXls() throws Exception{

  //声明一个工作薄

  HSSFWorkbook wb = new HSSFWorkbook();

  //声明表

  HSSFSheet sheet = wb.createSheet("第一个表");

  //声明行

  HSSFRow row = sheet.createRow(7);

  //声明列

  HSSFCell cel = row.createCell(3);

  //写入数据

  cel.setCellValue("你也好");

  FileOutputStream fileOut = new FileOutputStream("d:/a/b.xls");

  wb.write(fileOut);

  fileOut.close();

  }

  @Test

  public void export() throws Exception{

  //声明需要导出的数据库

  String dbName = "contact";

  //声明book

  HSSFWorkbook book = new HSSFWorkbook();

  //获取Connection,获取db的元数据

  Connection con = DataSourceUtils.getConn();

  //声明statemen

  Statement st = con.createStatement();

  //st.execute("use "+dbName);

  DatabaseMetaData dmd = con.getMetaData();

  //获取数据库有多少表

  ResultSet rs = dmd.getTables(null,null,null,new String[]{"TABLE"});

  //获取所有表名 - 就是一个sheet

  List tables = new ArrayList();

  while(rs.next()){

  String tableName = rs.getString("TABLE_NAME");

  System.out.println("**********" + tableName);

  tables.add(tableName);

  }

[1] [2] 

sqlserver数据写入到excel

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!