Java imports database data into EXCEL

Source: Internet
Author: User

Java imports database data into EXCEL

How do I import general database data into excel ?? This is confusing. Today I have found a way to implement it.

You need to import a third-party package

The details are as follows:

This document contains instructions on how to read database files to excel and excel databases in index.html.

A package is mainly used.

Copy the package to the project.

First make a simple version of helloword

 

[Html] view plaincopyprint?
  1. Span> public void mkexcel () throws Exception {
  2. // Create a working thin instance in the memory
  3. HSSFWorkbook book = new HSSFWorkbook (); // create a workbook
  4. HSSFSheet sheet = book. createSheet ("table 1"); // form name and form
  5. HSSFRow row = sheet. createRow (4); // number of rows created
  6. HSSFCell cell = row. createCell (4); // create the number of Columns
  7. Cell. setCellValue ("Big Brother, come on !! "); // Write the data to the cell
  8. // Cannot be created in the memory. It must be written to a file.
  9. FileOutputStream out = new FileOutputStream ("a.xls ");
  10. Book. write (out );
  11. }
  12.  
: 

 

Through the simple example above, we can find that the file used to create xls is similar to dynamically creating a table on a webpage.

1. Create a work thin

2. Create a thin table

3. Table Construction Bank

4. Create rows and columns

5. set values in the column

With this simple introduction above, we will start to import the content in the database.

1. We must create a working thin from the memory.

 

[Html] view plaincopyprint?
  1. Span> HSSFWorkbook book = new HSSFWorkbook (); span>

 

 

2. When creating a sheet, we need to specify the sheet name. All the table names in the database need to be obtained through metadata.

 

[Html] view plaincopyprint?
  1. Span> Connection con = hibernateFactory2.getCon (); // The database Connection is successful. span>

DatabaseMetaData dmeta = con. getMetaData (); // metadata

 

[Html] view plaincopyprint? 
  1. Span> ResultSet rs = dmeta. getCatalogs (); // obtain all the databases. For convenience, specify the database name span>
  2. Span>

 

 

3. Get the table name,

 

[Html] view plaincopyprint?
  1. Span> String dName = "hncu ";
  2. // Obtain the table name in the database and put all the returned resultsets in the list
  3. ResultSet rs = dmeta. getTables (dName, dName, null, new String [] {"TABLE "});
  4. // Put it in the list, with the sheet name following it
  5. List List = new ArrayList ();
  6. While (rs. next ())
  7. {

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.