java jxl export data from database to excel

package demo;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * Import the contents of the database into excel, you can paginate
 * 用到 jxl.jar ojdbc.jar
 * @author gr
 *
 */

public class ExcelImportDemo {
	static ResultSet rs = null;
	static ResultSetMetaData rsData = null;
	static int sheetIndex = 0; // counter, starting at 0
	static int perPageSize = 60000;// excel2003 has a maximum of 65536 rows per sheet
	static String fileName = "text.xls";// New table name
	static String URL = "jdbc:oracle:thin:@172.16.200.34:1521:orcl";// Database IP address
	static String DriverName = "oracle.jdbc.driver.OracleDriver";
	static String username = "riskcontrol";// 用户
	static String password = "riskcontrol";// 密码

	public static void main(String[] args) throws Exception {
		connectOracle();

	}

	static void connectOracle() throws SQLException, ClassNotFoundException {
		Connection conn = null;
		Class.forName(DriverName);
		long begintime = System.currentTimeMillis();
		try {
			System.out.println("Database connection started:" + new Date(System.currentTimeMillis()));
			conn = DriverManager.getConnection(URL, username, password);
			Statement state = conn.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			String sql = "select * from TAW_RISK_ASSIST_FUNCTIONPARAM";
			rs = state.executeQuery(sql);
			rsData = rs.getMetaData();// Get the required rs information

			rs.last();
			int size = rs.getRow();// 列数
			rs.beforeFirst();
			int sheetsize = size / perPageSize + 1;// sheetsize is the total number of pages in the sheet

			writeexcel(rs);// Create a new excel and write to the first sheet, only one sheet can be written at a time
			for (int i = 1; i < sheetsize; i++) {
				System.out.println(2);
				updateexcel(rs);//Write to the following table
			}
		} catch (Exception e) {
			System.out.println("Database connection error");
			e.printStackTrace ();
		} finally {
			conn.close();
			System.out.println("Database connection closed! Time: "
					+ (System.currentTimeMillis() - begintime) + "ms");
		}
	}

	static void writeexcel(ResultSet rs) throws IOException, Exception,
			WriteException {
		WritableWorkbook book = Workbook.createWorkbook(new File(fileName));
		WritableSheet sheet = book.createSheet("第" + (sheetIndex + 1) + "页",
				sheetIndex);
		int i = 1; // set each page as the first row
		while (rs.next()) {
			for (int j = 1; j < rsData.getColumnCount(); j++) {
				Label label = new Label(j - 1, i, rs.getString(j) == null ? ""
						: rs.getString(j).toString());
				if (i == 1) {// The title is only loaded the first time
					Label titleLabel = new Label(j - 1, 0, rsData
							.getColumnName(j));
					sheet.addCell(titleLabel);
				}
				sheet.addCell(label);
			}
			i++;
			if (i > perPageSize) {
				break;
			}
		}
		sheetIndex++;
		book.write();
		book.close();
	}

	static void updateexcel(ResultSet rs) throws BiffException, IOException,
			RowsExceededException, WriteException, SQLException {
		Workbook wb = Workbook.getWorkbook(new File(fileName));
		WritableWorkbook book = Workbook.createWorkbook(new File(fileName), wb);

		WritableSheet sheet = book.createSheet("第" + (sheetIndex + 1) + "页 ",
				sheetIndex);
		int i = 1; // set each page as the first row
		while (rs.next()) {
			for (int j = 1; j < rsData.getColumnCount(); j++) {
				Label label = new Label(j - 1, i, rs.getString(j) == null ? ""
						: rs.getString(j).toString());
				if (i == 1) {// The title is only loaded the first time
					Label titleLabel = new Label(j - 1, 0, rsData
							.getColumnName(j));
					sheet.addCell(titleLabel);
				}
				sheet.addCell(label);
			}
			i++;
			if (i > perPageSize) {
				break;
			}
		}
		sheetIndex++;
		book.write();
		book.close();

	}
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324879198&siteId=291194637