java 从数据库查出数据,jxl导出Excel

相关笔记:java jxl读取Excel文件数据插入数据库

--------------------------------------------------------------------------------------------------------

1、下载jxl.jar包,下载网址:https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl/2.6.12

2、数据库连接。

我本地安装了Oracle数据库,数据库名为:orcl,用户名为:system,密码为:Deng123456,下面是连接方式

	/**
	 * 数据库连接
	 * @throws Exception
	 */
	public static void dataBaseConnection() throws Exception{
		System.out.println("连接数据库开始......");
		Class.forName("oracle.jdbc.driver.OracleDriver");
		 conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","Deng123456");
		 System.out.println("连接数据库成功......");
	}

3、从数据库中查出数据,写入Excel文件中。

	/**
	 * 从数据库中查出数据,写入Excel文件中
	 * @throws Exception
	 */
	public static void createExcel() throws Exception{
		String sFilePath = "F:/study/20190713/";//文件路径
		String sFileName = "test.xls";//文件名
		File fFlie = new File(sFilePath+sFileName);
		WritableWorkbook book= Workbook.createWorkbook(fFlie);
		String sSheetName = "学生信息";//工作表名称
		WritableSheet sheet = book.createSheet(sSheetName,0);
		sheet.getSettings().setShowGridLines(false);//去掉工作表网格线
		
		//定义样式
		WritableFont font = new WritableFont(WritableFont.ARIAL, 10);
		WritableCellFormat format = new WritableCellFormat(font);
		format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
		format.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直居中对齐
		format.setBackground(Colour.GRAY_25);//设置背景颜色
		format.setBorder(Border.ALL, BorderLineStyle.THIN);//设置单元格边框
		//定义表头
		String[] sTitle = {"序号","学号","姓名","年龄","性别","QQ","联系电话","家庭地址"};
		
		//设置各列列宽
		for(int i = 0; i < sTitle.length; i++){
			if(i==7){
				sheet.setColumnView(i, 40); 
			}else{
				sheet.setColumnView(i, 15); 
			}
		}
		
		int iRow = 0;//行号
		for(int i = 0; i < sTitle.length; i++){
			sheet.addCell(new Label(i,iRow,sTitle[i],format)); 
		}
		iRow += 1;
		String sSql = " select stu_no,stu_name,stu_age,stu_sex,stu_phone,stu_qq,stu_addr from student ";
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery(sSql);
		while(rs.next()) {
			sheet.addCell(new Label(0,iRow,iRow+"",format));
			int iCol = 1;//列号
			String sColumnValue = "";
			while(iCol <= rs.getMetaData().getColumnCount()){//列号不能大于查询结果集列数,否则报无效索引异常
				sColumnValue = rs.getString(iCol);
				sheet.addCell(new Label(iCol,iRow,sColumnValue,format)); 
				iCol ++;
			}
			iRow ++;
		}
		rs.close();
		st.close();
		conn.close();//关闭连接
		
		//写入数据
		book.write();
		//关闭文件
		book.close();
		System.out.println("生成文件结束......");
	}

4、数据库中数据为:

java 从数据库查出数据,jxl导出Excel_第1张图片

生成Excel文件数据显示:

java 从数据库查出数据,jxl导出Excel_第2张图片

5、整体代码:

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

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class MyData2Excel {
	
	static Connection conn = null;
	
	public static void main(String[] args) throws Exception {
		try {
			dataBaseConnection();
			createExcel();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 数据库连接
	 * @throws Exception
	 */
	public static void dataBaseConnection() throws Exception{
		System.out.println("连接数据库开始......");
		Class.forName("oracle.jdbc.driver.OracleDriver");
		 conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","Deng123456");
		 System.out.println("连接数据库成功......");
	}
	
	/**
	 * 从数据库中查出数据,写入Excel文件中
	 * @throws Exception
	 */
	public static void createExcel() throws Exception{
		String sFilePath = "F:/study/20190713/";//文件路径
		String sFileName = "test.xls";//文件名
		File fFlie = new File(sFilePath+sFileName);
		WritableWorkbook book= Workbook.createWorkbook(fFlie);
		String sSheetName = "学生信息";//工作表名称
		WritableSheet sheet = book.createSheet(sSheetName,0);
		sheet.getSettings().setShowGridLines(false);//去掉工作表网格线
		
		//定义样式
		WritableFont font = new WritableFont(WritableFont.ARIAL, 10);
		WritableCellFormat format = new WritableCellFormat(font);
		format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
		format.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直居中对齐
		format.setBackground(Colour.GRAY_25);//设置背景颜色
		format.setBorder(Border.ALL, BorderLineStyle.THIN);//设置单元格边框
		//定义表头
		String[] sTitle = {"序号","学号","姓名","年龄","性别","QQ","联系电话","家庭地址"};
		
		//设置各列列宽
		for(int i = 0; i < sTitle.length; i++){
			if(i==7){
				sheet.setColumnView(i, 40); 
			}else{
				sheet.setColumnView(i, 15); 
			}
		}
		
		int iRow = 0;//行号
		for(int i = 0; i < sTitle.length; i++){
			sheet.addCell(new Label(i,iRow,sTitle[i],format)); 
		}
		iRow += 1;
		String sSql = " select stu_no,stu_name,stu_age,stu_sex,stu_phone,stu_qq,stu_addr from student ";
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery(sSql);
		while(rs.next()) {
			sheet.addCell(new Label(0,iRow,iRow+"",format));
			int iCol = 1;//列号
			String sColumnValue = "";
			while(iCol <= rs.getMetaData().getColumnCount()){//列号不能大于查询结果集列数,否则报无效索引异常
				sColumnValue = rs.getString(iCol);
				sheet.addCell(new Label(iCol,iRow,sColumnValue,format)); 
				iCol ++;
			}
			iRow ++;
		}
		rs.close();
		st.close();
		conn.close();//关闭连接
		
		//写入数据
		book.write();
		//关闭文件
		book.close();
		System.out.println("生成文件结束......");
	}

}

 

你可能感兴趣的:(SQL笔记,Java学习笔记)