com.formkiq.server.service.ExportServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of formkiq-server Show documentation
Show all versions of formkiq-server Show documentation
Server-side integration for the FormKiQ ios application
package com.formkiq.server.service;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.formkiq.server.service.dto.FormJSON;
import com.formkiq.server.service.dto.FormJSONField;
import com.formkiq.server.service.dto.FormJSONSection;
/**
* Implementation of Export Service.
*
*/
@Service
public class ExportServiceImpl implements ExportService {
@Override
public void writeToXLS(final FormJSON form, final OutputStream os)
throws IOException {
Workbook wb = new HSSFWorkbook();
try {
writeToExcel(os, form, wb);
} finally {
wb.close();
}
}
@Override
public void writeToXLSX(final FormJSON form, final OutputStream os)
throws IOException {
Workbook wb = new XSSFWorkbook();
try {
writeToExcel(os, form, wb);
} finally {
wb.close();
}
}
/**
* Writes to Excel.
* @param os OutputStream
* @param form FormJSON
* @param wb Workbook
* @throws IOException IOException
*/
private void writeToExcel(final OutputStream os, final FormJSON form,
final Workbook wb) throws IOException {
int count = 1;
for (FormJSONSection section : form.getSections()) {
String title = getSectionTitle(section, count);
Sheet sheet = wb.createSheet(title);
// header row
Row headerRow = sheet.createRow(0);
int column = 0;
for (FormJSONField field : section.getFields()) {
Cell cell = headerRow.createCell(column);
cell.setCellValue(field.getLabel());
column++;
}
column = 0;
Row dataRow = sheet.createRow(1);
for (FormJSONField field : section.getFields()) {
Cell cell = dataRow.createCell(column);
cell.setCellValue(field.getValue());
column++;
}
count++;
}
wb.write(os);
}
/**
* Gets Section Title.
* @param section FormJSONSection
* @param count int
* @return {@link String}
*/
private String getSectionTitle(final FormJSONSection section,
final int count) {
String title = !StringUtils.isEmpty(section.getTitle()) ? section
.getTitle() : "sheet " + count;
return title;
}
}