以上代码请写完整,包括从数据库查询到导出
时间: 2024-03-05 18:48:26 浏览: 26
好的,根据您的要求,以下是完整的示例代码,包括从数据库查询数据到导出Excel的全过程。在这个示例中,我使用了MySQL数据库和JDBC驱动,您可以根据自己的实际情况进行修改:
```java
import java.io.FileOutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExportExcelWithGroupAndMerge {
public static void main(String[] args) throws Exception {
// 1. 加载JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 建立数据库连接
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
// 3. 执行SQL查询
String sql = "SELECT * FROM student ORDER BY grade, class";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
// 4. 处理查询结果
List<Map<String, Object>> dataList = new ArrayList<>();
while (rs.next()) {
Map<String, Object> data = new HashMap<>();
data.put("id", rs.getInt("id"));
data.put("name", rs.getString("name"));
data.put("gender", rs.getString("gender"));
data.put("grade", rs.getInt("grade"));
data.put("class", rs.getInt("class"));
data.put("score", rs.getDouble("score"));
dataList.add(data);
}
rs.close();
stmt.close();
conn.close();
// 5. 对查询结果进行分组和合并单元格
Map<String, List<Map<String, Object>>> groupMap = new HashMap<>();
for (Map<String, Object> data : dataList) {
String groupKey = data.get("grade") + "年级" + data.get("class") + "班";
if (!groupMap.containsKey(groupKey)) {
groupMap.put(groupKey, new ArrayList<Map<String, Object>>());
}
groupMap.get(groupKey).add(data);
}
// 6. 创建Excel工作簿和工作表
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("学生成绩表");
int rowIndex = 0;
// 7. 输出分组后的数据到Excel中
for (Map.Entry<String, List<Map<String, Object>>> entry : groupMap.entrySet()) {
// 输出分组名称
Row groupRow = sheet.createRow(rowIndex++);
Cell groupCell = groupRow.createCell(0);
groupCell.setCellValue(entry.getKey());
// 合并单元格
sheet.addMergedRegion(new CellRangeAddress(
groupRow.getRowNum(), groupRow.getRowNum(),
0, 5)); // 合并到最后一列
// 输出分组内的数据
for (Map<String, Object> data : entry.getValue()) {
Row dataRow = sheet.createRow(rowIndex++);
// 输出各个字段的值
Cell cell1 = dataRow.createCell(0);
cell1.setCellValue(data.get("id").toString());
Cell cell2 = dataRow.createCell(1);
cell2.setCellValue(data.get("name").toString());
Cell cell3 = dataRow.createCell(2);
cell3.setCellValue(data.get("gender").toString());
Cell cell4 = dataRow.createCell(3);
cell4.setCellValue(data.get("grade").toString());
Cell cell5 = dataRow.createCell(4);
cell5.setCellValue(data.get("class").toString());
Cell cell6 = dataRow.createCell(5);
cell6.setCellValue(data.get("score").toString());
}
}
// 8. 将Excel导出到文件或者输出流中
FileOutputStream fos = new FileOutputStream("学生成绩表.xlsx");
workbook.write(fos);
fos.close();
System.out.println("导出成功!");
}
}
```
在这个示例中,我查询了一个名为“student”的表,包含学生的id、姓名、性别、年级、班级和分数等字段。按照年级和班级进行分组,并将每个分组内的数据按照id、姓名、性别、年级、班级和分数的顺序输出到Excel中。注意,在输出分组名称时,我使用了“年级+班级”的形式,并将分组后的数据按照这个形式进行了合并单元格。
希望这个示例能够帮助到您!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)