`//如何将动态表数据转储到excel sheet springboot的示例代码//如何将动态表数据转储到excel sheet springboot //如何将动态表数据转储到excel sheet springboot
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/excel")
public class ExcelController {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/download")
public ModelAndView downloadExcel(HttpServletResponse response) throws IOException, SQLException {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=table-data.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook();
int sheetNumber = 1;
int rowNumber = 0;
int rowPerPage = 10000; // maximum number of rows per sheet
List<String> columnNames = getColumnNames();
XSSFSheet sheet = workbook.createSheet("Sheet " + sheetNumber);
createHeaderRow(sheet, columnNames);
ResultSet resultSet = getResultSet();
while (resultSet.next()) {
if (rowNumber >= rowPerPage) {
sheetNumber++;
sheet = workbook.createSheet("Sheet " + sheetNumber);
createHeaderRow(sheet, columnNames);
rowNumber = 0;
}
List<Object> rowValues = getRowValues(resultSet);
createDataRow(sheet, rowNumber, rowValues);
rowNumber++;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
return new ModelAndView("excelView", "excelData", baos.toByteArray());
}
private List<String> getColumnNames() {
List<String> columnNames = new ArrayList<>();
jdbcTemplate.query("SELECT column_name FROM information_schema.columns WHERE table_name = 'my_table'", (rs, rowNum) -> {
columnNames.add(rs.getString("column_name"));
return null;
});
return columnNames;
}
private ResultSet getResultSet() throws SQLException {
return jdbcTemplate.getDataSource().getConnection().createStatement().executeQuery("SELECT * FROM my_table");
}
private List<Object> getRowValues(ResultSet resultSet) throws SQLException {
List<Object> rowValues = new ArrayList<>();
int columnCount = resultSet.getMetaData().getColumnCount();
for (int i = 1; i <= columnCount; i++) {
rowValues.add(resultSet.getObject(i));
}
return rowValues;
}
private void createHeaderRow(XSSFSheet sheet, List<String> columnNames) {
int cellNumber = 0;
for (String columnName : columnNames) {
sheet.createRow(0).createCell(cellNumber).setCellValue(columnName);
cellNumber++;
}
}
private void createDataRow(XSSFSheet sheet, int rowNumber, List<Object> rowValues) {
int cellNumber = 0;
for (Object value : rowValues) {
sheet.createRow(rowNumber + 1).createCell(cellNumber).setCellValue(value.toString());
cellNumber++;
}
}
} //here
` ``//如何将动态表数据转储到excel sheet springboot的示例代码//如何将动态表数据转储到excel sheet springboot //如何将动态表数据转储到excel sheet springboot
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/excel")
public class ExcelController {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/download")
public ModelAndView downloadExcel(HttpServletResponse response) throws IOException, SQLException {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=table-data.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook();
int sheetNumber = 1;
int rowNumber = 0;
int rowPerPage = 10000; // maximum number of rows per sheet
List<String> columnNames = getColumnNames();
XSSFSheet sheet = workbook.createSheet("Sheet " + sheetNumber);
createHeaderRow(sheet, columnNames);
ResultSet resultSet = getResultSet();
while (resultSet.next()) {
if (rowNumber >= rowPerPage) {
sheetNumber++;
sheet = workbook.createSheet("Sheet " + sheetNumber);
createHeaderRow(sheet, columnNames);
rowNumber = 0;
}
List<Object> rowValues = getRowValues(resultSet);
createDataRow(sheet, rowNumber, rowValues);
rowNumber++;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
return new ModelAndView("excelView", "excelData", baos.toByteArray());
}
private List<String> getColumnNames() {
List<String> columnNames = new ArrayList<>();
jdbcTemplate.query("SELECT column_name FROM information_schema.columns WHERE table_name = 'my_table'", (rs, rowNum) -> {
columnNames.add(rs.getString("column_name"));
return null;
});
return columnNames;
}
private ResultSet getResultSet() throws SQLException {
return jdbcTemplate.getDataSource().getConnection().createStatement().executeQuery("SELECT * FROM my_table");
}
private List<Object> getRowValues(ResultSet resultSet) throws SQLException {
List<Object> rowValues = new ArrayList<>();
int columnCount = resultSet.getMetaData().getColumnCount();
for (int i = 1; i <= columnCount; i++) {
rowValues.add(resultSet.getObject(i));
}
return rowValues;
}
private void createHeaderRow(XSSFSheet sheet, List<String> columnNames) {
int cellNumber = 0;
for (String columnName : columnNames) {
sheet.createRow(0).createCell(cellNumber).setCellValue(columnName);
cellNumber++;
}
}
private void createDataRow(XSSFSheet sheet, int rowNumber, List<Object> rowValues) {
int cellNumber = 0;
for (Object value : rowValues) {
sheet.createRow(rowNumber + 1).createCell(cellNumber).setCellValue(value.toString());
cellNumber++;
}
}
} //here
``
5条答案
按热度按时间yuvru6vn1#
4xy9mtcn2#
hi3rlvi23#
x3naxklr4#
mpgws1up5#