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

Mifeet
  • 12,949
  • 5
  • 60
  • 108
Op Kaushik
  • 140
  • 2
  • 19
  • Possible duplicate of [How to implement Export sqlite To excel/csv file in android?](http://stackoverflow.com/questions/21448001/how-to-implement-export-sqlite-to-excel-csv-file-in-android) – Mifeet May 16 '16 at 12:38

1 Answers1

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

jakob
  • 5,979
  • 7
  • 64
  • 103
  • 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. – Ved Oct 07 '13 at 12:28