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?
- Span> public void mkexcel () throws Exception {
- // Create a working thin instance in the memory
- HSSFWorkbook book = new HSSFWorkbook (); // create a workbook
- HSSFSheet sheet = book. createSheet ("table 1"); // form name and form
- HSSFRow row = sheet. createRow (4); // number of rows created
- HSSFCell cell = row. createCell (4); // create the number of Columns
- Cell. setCellValue ("Big Brother, come on !! "); // Write the data to the cell
- // Cannot be created in the memory. It must be written to a file.
- FileOutputStream out = new FileOutputStream ("a.xls ");
- Book. write (out );
- }
-
:
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?
- 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?
- Span> Connection con = hibernateFactory2.getCon (); // The database Connection is successful. span>
DatabaseMetaData dmeta = con. getMetaData (); // metadata
[Html] view plaincopyprint?
- Span> ResultSet rs = dmeta. getCatalogs (); // obtain all the databases. For convenience, specify the database name span>
- Span>
3. Get the table name,
[Html] view plaincopyprint?
- Span> String dName = "hncu ";
- // Obtain the table name in the database and put all the returned resultsets in the list
- ResultSet rs = dmeta. getTables (dName, dName, null, new String [] {"TABLE "});
- // Put it in the list, with the sheet name following it
- List List = new ArrayList ();
- While (rs. next ())
- {