본문 바로가기

Web

엑셀(JXL) 다운로드

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Dependency

 

<dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
</dependency>





Servlet Context Xml


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd    
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="viewResolver1" class="org.springframework.web.servlet.view.XmlViewResolver">

          <property name="order" value="1"/>
          <property name="location" value="/WEB-INF/excel_view.xml"/>
    </bean>




Builder Xml  (excel_view.xml)

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="excelView" class="com.chience.odin.excel.ExcelBuilder" />
</beans>






JAVASCRIPT 

 


$("#버튼ID").click(function(){
        location.href = "downloadExcel.xls?table_start_time=" + table_start_time +
                        "&table_end_time=" + table_end_time +
                        "&table_select_name=" + table_select_name;        
});






CONTROLLER

 

@Controller
public class ExcelControl {
    @Autowired
    private DataService dataService; //DataBase Service
        
    @RequestMapping(value = "/downloadExcel.xls", method = RequestMethod.GET)
    public ModelAndView downloadExcel(HttpServletRequest request,HttpServletResponse response, Principal principal) {
                        
        //data         
        List<RawDataDto> list = dataService.select_table_all(parameter);

        String A,B,C ~~~~;   
        model.put("list", list);
        model.put("A", A);

        .......

        
        return new ModelAndView("excelView", "model", model);        
    }
}






Builder Class

 

public class ExcelBuilder extends AbstractJExcelView {    
    @Override
    protected void buildExcelDocument(Map<String, Object> model,
            WritableWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        @SuppressWarnings("unchecked")
        Map<String, Object> dump = (HashMap<String, Object>) model.get("model");
        
        @SuppressWarnings("unchecked")
        List<RawDataDto> list = (List<RawDataDto>) dump.get("list");
        String title = (String) dump.get("title");
        
        // create a new Excel sheet
        WritableSheet sheet = workbook.createSheet("Raw Data", 0);
        
        //title add
        sheet.mergeCells(0, 0, 2, 0 );
        sheet.addCell(new Label(0, 0, title));
        
        // create header row
        sheet.addCell(new Label(0, 1, "Time"));
        sheet.addCell(new Label(1, 1, "Value"));
        sheet.addCell(new Label(2, 1, "RSSI"));
        
        // create data rows
        int rowCount = 2;
        
        for (RawDataDto alist : list) {
            sheet.addCell(new Label(0, rowCount, alist.getTime()));
            sheet.addCell(new Label(1, rowCount, alist.getValue()));
            sheet.addCell(new Label(2, rowCount, alist.getRSSI()));            
            rowCount++;
        }
         sheet.setColumnView(0, 30); 
         sheet.setColumnView(1, 30);
         sheet.setColumnView(2, 30);
                  
    }
}





테스트 진행


개발중인 웹페이지에서 버튼클릭 후 다운로드 확인



열어보자!!!!





매우 만족스럽다.


이제 실시간 HDFS에 저장되는 다수의 센서 데이터를 사용자가 원하는 기간/노드/센서/저장 컬럼명 별로 선택 후 정리해서 엑셀파일로 제공 가능











'Web' 카테고리의 다른 글

rabbitmq - web  (0) 2018.02.09
tomcat + apache 연동 (mok_jk)  (0) 2017.11.08
centos7 + tomcat8 자동실행  (0) 2017.11.08
Spring Security  (0) 2017.06.12
Web Socket 사용법  (1) 2017.04.25