mysql查询保存到excel文件_如何将mysql查询输出保存到excel或.txt文件?

MySQL provides an easy mechanism for writing the results of a select

statement into a text file on the server. Using extended options of

the INTO OUTFILE nomenclature, it is possible to create a comma

separated value (CSV) which can be imported into a spreadsheet

application such as OpenOffice or Excel or any other applciation which

accepts data in CSV format.

Given a query such as

SELECT order_id,product_name,qty FROM orders

which returns three columns of data, the results can be placed into

the file /tmo/orders.txt using the query:

SELECT order_id,product_name,qty FROM orders

INTO OUTFILE '/tmp/orders.txt'

This will create a tab-separated file, each row on its own line. To

alter this behavior, it is possible to add modifiers to the query:

SELECT order_id,product_name,qty FROM orders

INTO OUTFILE '/tmp/orders.csv'

FIELDS TERMINATED BY ','

ENCLOSED BY '"'

LINES TERMINATED BY '\n'

In this example, each field will be enclosed in “double quotes,” the

fields will be separated by commas, and each row will be output on a

new line separated by a newline (\n). Sample output of this command

would look like:

"1","Tech-Recipes sock puppet","14.95" "2","Tech-Recipes chef's hat","18.95"

Keep in mind that the output file must not already exist and that the

user MySQL is running as has write permissions to the directory MySQL

is attempting to write the file to.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Apache POI 库实现将 MySQL 查询结果导出到 Excel 文件中。下面是一个简单的示例代码: 1. 添加 Maven 依赖 ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建 Excel 文件并写入数据 ```java // 创建工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 执行查询并获取结果集 List<Object[]> resultList = jdbcTemplate.query(sql, args, new BeanPropertyRowMapper<>(clazz)); // 写入表头 Row headerRow = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(headers[i]); } // 写入数据 int rowIndex = 1; for (Object[] row : resultList) { Row dataRow = sheet.createRow(rowIndex++); for (int i = 0; i < row.length; i++) { Cell cell = dataRow.createCell(i); cell.setCellValue(row[i].toString()); } } // 输出 Excel 文件 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); ``` 其中,`jdbcTemplate` 是 Spring 提供的 JDBC 操作工具类,`sql` 是要执行的 SQL 语句,`args` 是 SQL 语句中的参数,`clazz` 是查询结果对应的实体类,`headers` 是表头数组,`fileName` 是导出的 Excel 文件名,`response` 是 HttpServletResponse 对象。 以上代码将查询结果写入 Excel 文件并直接输出到浏览器。你可以根据实际需求自行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值