0

I have new hand on Android. I have made a report using ListView which is divided into three columns named DateTime, OnOffStatus, AlarmImage.

It work fine and looks good enough but now I want to export this table data to Excel format. Is it possible or not and how?

thanks in advance Om Parkash Kaushik

1

1 Answer 1

1

Start of by looking at http://poi.apache.org/ and http://poi.apache.org/spreadsheet/index.html I use this library to all of my excel reports.

Start of by creating a Workbook:

HSSFWorkbook workbook = new HSSFWorkbook();
Map<String, CellStyle> styles = createStyles(workbook);
HSSFSheet sheet = workbook.createSheet();

Setup some style:

private static Map<String, CellStyle> createStyles(Workbook wb) {

Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style;
Font monthFont = wb.createFont();
monthFont.setFontHeightInPoints((short) 11);
monthFont.setColor(IndexedColors.WHITE.getIndex());
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(monthFont);
style.setWrapText(true);

styles.put("header", style);
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setWrapText(true);
style.setBorderRight(CellStyle.BORDER_NONE);
style.setBorderLeft(CellStyle.BORDER_NONE);
style.setBorderTop(CellStyle.BORDER_NONE);
style.setBorderBottom(CellStyle.BORDER_NONE);
styles.put("cell", style);
    return styles;
}

Then setup a header row:

private static final String[] article_headers = {"header1", "header2"};

// Header row
Row headerRow = sheet.createRow(0);
headerRow.setHeightInPoints(40);
Cell headerCell;

for (int i = 0; i < article_headers.length; i++) {
    headerCell = headerRow.createCell(i);
    headerCell.setCellValue(article_headers[i]);
    headerCell.setCellStyle(styles.get("header"));
}

And then you continue with the rows by setting their style and value.

Hope this helps and if you find this helpful, remember to accept.

// Jakob

Sign up to request clarification or add additional context in comments.

1 Comment

I got the "java.lang.NoClassDefFoundError: org.apache.poi.hssf.usermodel.HSSFWorkbook" error when I run this code in my application.Can you guide me how to remove this error from my code.I have added poi-3.9-20121203.jar file in my project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.