Java利用Apache POI將數據庫數據導出為excel


將數據庫中的數據導出為excel文件,供其他人查看

public class POITest {

public static void main(String[] args) {
POITest test
= new POITest();

// test.readExcelToDB();

test.writeExcelFromDB();

}

static class Book{
public String title;
public String author;
public String date;
}

SimpleDateFormat format
= new SimpleDateFormat("yyyy/MM/dd");
ComboPooledDataSource dataSource;
public POITest() {
//初始化數據庫連接池
try {
dataSource
= new ComboPooledDataSource();
dataSource.setDriverClass(
"com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl(
"jdbc:mysql://192.168.0.208:3306/test?user=root&password=qazxswedc123"
+ "&characterEncoding=utf8&serverTimezone=UTC");
}
catch (PropertyVetoException e) {
e.printStackTrace();
}
}

//從數據庫讀取數據並保存為excel
public void writeExcelFromDB(){
List
<Book> books = new ArrayList<POITest.Book>();
try {
Connection conn
= dataSource.getConnection();
Statement statement
= conn.createStatement();
ResultSet resultSet
= statement.executeQuery("select * from book");
while(resultSet.next()){
Book book
= new Book();
//第一列是
book.title = resultSet.getString(2);
book.author
= resultSet.getString(3);
book.date
= format.format(resultSet.getDate(4));
books.add(book);
}

writeExcel(books);
}
catch (SQLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}

}

//HSSF 寫excel
private void writeExcel(List<Book> books) throws IOException{
HSSFWorkbook workbook
= new HSSFWorkbook();
//創建表
HSSFSheet sheet = workbook.createSheet("書本");
//創建首行
HSSFRow topRow = sheet.createRow(0);
//創建首行單元格樣式
HSSFCellStyle topCellStyle = workbook.createCellStyle();
topCellStyle.setAlignment(HorizontalAlignment.CENTER);
topCellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
topCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
topCellStyle.setBorderBottom(BorderStyle.THIN);
topCellStyle.setBorderLeft(BorderStyle.THIN);
topCellStyle.setBorderTop(BorderStyle.THIN);
topCellStyle.setBorderRight(BorderStyle.THIN);
HSSFFont topFont
= workbook.createFont();
topFont.setColor(HSSFColor.BLACK.index);
topCellStyle.setFont(topFont);

HSSFCell topCell
= topRow.createCell(0);
topCell.setCellValue(
"書名");
topCell.setCellStyle(topCellStyle);
topCell
= topRow.createCell(1);
topCell.setCellValue(
"作者");
topCell.setCellStyle(topCellStyle);
topCell
= topRow.createCell(2);
topCell.setCellValue(
"出版日期");
topCell.setCellStyle(topCellStyle);

//設置普通行單元格樣式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.cloneStyleFrom(topCellStyle);
cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
HSSFFont font
= workbook.createFont();
font.setColor(HSSFColor.BLACK.index);
cellStyle.setFont(font);

for (int i=0; i<books.size(); i++) {
HSSFRow row
= sheet.createRow(i+1);
Book book
= books.get(i);
HSSFCell cell
= row.createCell(0);
cell.setCellValue(book.title);
cell.setCellStyle(cellStyle);
cell
= row.createCell(1);
cell.setCellValue(book.author);
cell.setCellStyle(cellStyle);
cell
= row.createCell(2);
cell.setCellValue(book.date);
cell.setCellStyle(cellStyle);
}

FileOutputStream os
= new FileOutputStream("d:/book.xls");
workbook.write(os);
os.flush();
os.close();
}


//從本地讀取excel數據插入數據庫
public void readExcelToDB(){
try {
List
<Book> books = readExcel();
Connection conn
= dataSource.getConnection();
String sql
= "insert into book(title,author,submission_date) values(?,?,?)";
PreparedStatement preparedStatement
= conn.prepareStatement(sql);
for (Book book : books) {
preparedStatement.setString(
1, book.title);
preparedStatement.setString(
2, book.author);
preparedStatement.setDate(
3, new Date(format.parse(book.date).getTime()));
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
}
catch (SQLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ParseException e) {
e.printStackTrace();
}

}

//HSSF 讀excel
private List<Book> readExcel() throws IOException{
List
<Book> books = new ArrayList<POITest.Book>();
InputStream is
= new FileInputStream(new File("d:/book_2.xls"));
//得到工作薄
HSSFWorkbook workbook = new HSSFWorkbook(is);
//得到工作表
Sheet sheet = workbook.getSheetAt(0);
//得到行數
int rowNum = sheet.getLastRowNum();
//首行是標題行
for(int i=1; i<=rowNum; i++){
Book book
= new Book();
Row row
= sheet.getRow(i);
Cell cell
= row.getCell(0);
book.title
= cell.getStringCellValue();
cell
= row.getCell(1);
book.author
= cell.getStringCellValue();
cell
= row.getCell(2);
book.date
= format.format(cell.getDateCellValue());
books.add(book);
}

return books;
}

}

 

book建表語句

CREATE TABLE `book` (
`id`
int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
`title`
varchar(100) DEFAULT NULL,
`author`
varchar(40) DEFAULT NULL,
`submission_date` date
DEFAULT NULL
)

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com