java import mysql data as table

jar package preparation

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.17</version>
</dependency>

rear end

    @Override
    public List<Orders> selectList(Map<String,String> params) {

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        List<Orders> list = sqlSession.selectList("com.xwhbank.cn.config.mybatis.namespace.ordersMapper.selectList", params);

        return  list;

    }
  <select id="selectList" parameterType="java.util.Map" resultType="orders">
    select * from orders where the_super_id = #{the_super_id} and state = '2' and order_time >= #{daochu01} and order_time <= #{daochu02};
  </select>
List<Orders> list = ordersService.selectList(params);

Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("stuDB"); // Change stuDB to everything 
Row row = sheet.createRow(( short )0); // 0 Row This row sets the header of the table 
Cell cell = null ;
cell = row.createCell((short)0);
cell.setCellValue( "tenant_order_id"); // The name of the header of a column in the table 
cell = row.createCell(( short )1 );
cell.setCellValue("money");
cell = row.createCell((short)2);
cell.setCellValue("pay_way");
cell = row.createCell((short)3);
cell.setCellValue("order_time");
cell = row.createCell((short)4);
cell.setCellValue("schlep");

int i=1;
for (Orders o:list) {
    row = sheet.createRow(i);
    cell = row.createCell(0);
    cell.setCellValue(o.getTenant_order_no());
    cell = row.createCell(1);
    cell.setCellValue(o.getMoney_amount());
    cell = row.createCell(2);
    cell.setCellValue(o.getPay_way());
    cell = row.createCell(3);
    cell.setCellValue(o.getOrder_time());
    cell = row.createCell(4);
    cell.setCellValue(o.getSchlep());
    i++;
}
// The first header of HttpServletResponse must be set
 // The second one can be set first without setting only the first header. After running, you can determine what file suffix is ​​exported by the browser
 // If you need a custom name, you can add it Go to the second line and bring the determined file name suffix 
response.setHeader("content-Type", "application/vnd.ms-excel" );
 // The file name downloaded by the browser is set below to include the file suffix 
response.setHeader( "Content-Disposition", "attachment;filename="+the_super_id+"-"+ new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format( new Date())+".xls" );
workbook.write(response.getOutputStream());
response.getOutputStream().flush();
System.out.println("Success");

front end

// #dddc Anything clickable, #daochu01 needs to bring parameters to the server 
$("#dddc").on('click', function () {
             var url = "http://" + window.location. host + "/ddgl/dddc" ;
            $('<form method="get" action=' + url + '>' +
                '<input name="daochu01" type="text" value='+$("#daochu01").val()+'/>' +
                '<input name="daochu02" type="text" value='+$("#daochu02").val()+'/>' +
                '</form>').appendTo('body').submit().remove();
        });
// The ajax request server cannot return an excel form (this is uncertain), we need to convert the request into a page request, that is, convert it into a form form to send the request, so that the data can be exported as a form

result

5001263243584372792-2018_05_04 22_50_10.xls

 

Reference: https://www.tetwo.com/blog/7800

   https://www.cnblogs.com/yansj1997/p/4814213.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325299927&siteId=291194637
Recommended