-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportService.java
49 lines (35 loc) · 1.27 KB
/
ReportService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package in.ashokit;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ReportService {
@Autowired
private CourseRepository courseRepo;
public void generateExcel(HttpServletResponse response) throws Exception {
List<Course> courses = courseRepo.findAll();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Courses Info");
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("ID");
row.createCell(1).setCellValue("Name");
row.createCell(2).setCellValue("Price");
int dataRowIndex = 1;
for (Course course : courses) {
HSSFRow dataRow = sheet.createRow(dataRowIndex);
dataRow.createCell(0).setCellValue(course.getCid());
dataRow.createCell(1).setCellValue(course.getName());
dataRow.createCell(2).setCellValue(course.getPrice());
dataRowIndex++;
}
ServletOutputStream ops = response.getOutputStream();
workbook.write(ops);
workbook.close();
ops.close();
}
}