Java利用POI将数据库表导出到Excel
2017-09-15 22:31
453 查看
package outExcel;
import java.io.FileOutputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class toExcel {
public final static String outputFile="D:\\outFile\\outFile.xlsx"; //创建的Excel文件路径
public final static String url="jdbc:mysql://localhost:3308/shcydb"; //数据库路径
public final static String user="root";
//数据库用户名
public final static String password="mysql"; //数据库密码
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=(Connection) DriverManager.getConnection(url, user, password);
Statement stat = (Statement) conn.createStatement();
ResultSet resultSet = stat.executeQuery("SELECT * FROM news;"); // 查询表
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet("news");
XSSFRow row = sheet.createRow((short)0);
XSSFCell cell=null;
cell=row.createCell((short)0); //所需生成的列名
cell.setCellValue("news_id");
cell=row.createCell((short)1);
cell.setCellValue("user_id");
cell=row.createCell((short)2);
cell.setCellValue("news_content");
cell=row.createCell((short)3);
cell.setCellValue("news_crtdt");
int i=1;
while(resultSet.next())
{
row=sheet.createRow(i);
cell=row.createCell(0);
cell.setCellValue(resultSet.getString("news_id")); //所需生成的列名
cell=row.createCell(1);
cell.setCellValue(resultSet.getString("user_id"));
cell=row.createCell(2);
cell.setCellValue(resultSet.getString("news_content"));
cell=row.createCell(3);
cell.setCellValue(resultSet.getString("news_crtdt"));
i++;
}
FileOutputStream FOut = new FileOutputStream(outputFile);
workbook.write(FOut);
FOut.flush();
FOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
复件14个jar包
import java.io.FileOutputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class toExcel {
public final static String outputFile="D:\\outFile\\outFile.xlsx"; //创建的Excel文件路径
public final static String url="jdbc:mysql://localhost:3308/shcydb"; //数据库路径
public final static String user="root";
//数据库用户名
public final static String password="mysql"; //数据库密码
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=(Connection) DriverManager.getConnection(url, user, password);
Statement stat = (Statement) conn.createStatement();
ResultSet resultSet = stat.executeQuery("SELECT * FROM news;"); // 查询表
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet("news");
XSSFRow row = sheet.createRow((short)0);
XSSFCell cell=null;
cell=row.createCell((short)0); //所需生成的列名
cell.setCellValue("news_id");
cell=row.createCell((short)1);
cell.setCellValue("user_id");
cell=row.createCell((short)2);
cell.setCellValue("news_content");
cell=row.createCell((short)3);
cell.setCellValue("news_crtdt");
int i=1;
while(resultSet.next())
{
row=sheet.createRow(i);
cell=row.createCell(0);
cell.setCellValue(resultSet.getString("news_id")); //所需生成的列名
cell=row.createCell(1);
cell.setCellValue(resultSet.getString("user_id"));
cell=row.createCell(2);
cell.setCellValue(resultSet.getString("news_content"));
cell=row.createCell(3);
cell.setCellValue(resultSet.getString("news_crtdt"));
i++;
}
FileOutputStream FOut = new FileOutputStream(outputFile);
workbook.write(FOut);
FOut.flush();
FOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
复件14个jar包
相关文章推荐
- java 将 利用poi 数据库 内容 导出到excel 表格
- Java利用POI导入导出Excel中的数据
- java利用poi技术解析、导出excel
- java利用POI通过模板导出excel的一个实例
- Java 利用poi 导出Excel
- Java 利用poi把数据库中数据导入Excel
- Java 利用POI实现将数据库中内容导入到EXcel中
- java操作Excel之POI(4)利用POI实现数据的批量导出
- JAVA实现数据库数据导入/导出到Excel(POI)
- java使用poi导出excel的内容,同时可以利用反射进行动态获取信息
- java 利用org.apache.poi.hssf包编写excel导出文件
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- java利用poi导出数据到excel
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- Java利用POI导出Excel设置单元格格式
- java利用poi导出excel报表
- javaweb 利用poi 导出excel
- JAVA实现数据库数据导入/导出到Excel(POI技术)
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表