我有一个名为task list的活动。这个任务列表包含json数组 我现在有一个按钮,我想在按钮单击
上将任务列表导出到Excel表格中对于创建Excel工作表,我使用了poi-3.7.jar
我的Excel表格代码:
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.param11:
saveExcelFile(this,"myexcel.xls");
break;
}
}
private static boolean saveExcelFile(Context context, String fileName) {
// check if available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
Log.e(TAG, "Storage not available or read only");
return false;
}
boolean success = false;
//New Workbook
Workbook wb = new HSSFWorkbook();
Cell c = null;
//Cell style for header row
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(HSSFColor.LIME.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//New Sheet
Sheet sheet1 = null;
sheet1 = wb.createSheet("wrok");
// Generate column headings
org.apache.poi.ss.usermodel.Row row = sheet1.createRow(0);
c = row.createCell(0);
c.setCellValue("task_id");
c.setCellStyle(cs);
c = row.createCell(1);
c = row.createCell(2);
c.setCellValue("d_alias");
c.setCellStyle(cs);
c = row.createCell(3);
c.setCellValue("worktype_name");
c.setCellStyle(cs);
c = row.createCell(4);
c.setCellValue("status");
c.setCellStyle(cs);
c = row.createCell(5);
c.setCellValue("departmentstatus");
c.setCellStyle(cs);
c = row.createCell(6);
c.setCellValue("priority");
c.setCellStyle(cs);
c = row.createCell(7);
c.setCellValue("staff_name");
c.setCellStyle(cs);
sheet1.setColumnWidth(0, (15 * 500));
sheet1.setColumnWidth(1, (15 * 500));
sheet1.setColumnWidth(2, (15 * 500));
// Create a path where we will place our List of objects on external storage
File file = new File(context.getExternalFilesDir(null), fileName);
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
wb.write(os);
Log.w("FileUtils", "Writing file" + file);
success = true;
} catch (IOException e) {
Log.w("FileUtils", "Error writing " + file, e);
} catch (Exception e) {
Log.w("FileUtils", "Failed to save file", e);
} finally {
try {
if (null != os)
os.close();
} catch (Exception ex) {
}
}
return success;
}
public static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
public static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
///i have converted json string to csv
将json转换为csv我使用了org.json.jar
if (array.length() > 0) {
for (int i = 0; i < array.length(); i++) {
JSONObject obj = (JSONObject) array.get(i);
item.add(obj.getString("task_id").toString().trim());
WorkItem item = new WorkItem(obj.getString("id"),
obj.getString("task_id"),
obj.getString("firm_name"),
obj.getString("d_alias"),
obj.getString("worktype_name"),
obj.getString("status"),
obj.getString("departmentstatus"),
obj.getString("priority"),
obj.getString("staff_name"),
obj.getString("remark"));
String csv = CDL.toString(array);
System.out.println("csv " +csv); //csv
rowItems.add(item);
System.out.println("itemss" +item);
}
我的excelsheet已成功创建,但它只包含标题
我的json字符串:
object[{"id":"10","task_id":"drm\/Technical \/75","firm_name":"dreamhome","d_alias":"Technical ","worktype_name":"Technical","status":"completed","departmentstatus":"completed","priority":"high","staff_name":"anna","remark":"good","createddate":"2015-02-11 03:42:17","updateddate":"2015-02-11 03:42:17","isdeleted":"0"},{"id":"10","task_id":"sv\/Technical \/89","firm_name":"svye","d_alias":"Technical ","worktype_name":"aa","status":"Submitted","departmentstatus":"incomplete","priority":"5.0","staff_name":"sam","remark":"jdjd","createddate":"2015-02-12 23:59:05","updateddate":"2015-02-12 23:59:05","isdeleted":"0"}]
// csv string
02-18 14:41:43.531: I/System.out(854): 10,Technical,good,a,completed,high,0,Technical ,drm/Technical /75,2015-02-11 03:42:17,2015-02-11 03:42:17,completed,devrai
02-18 14:41:43.531: I/System.out(854): 10,aa,jdjd,Vishal,Submitted,5.0,0,Technical ,sv/Technical /89,2015-02-12 23:59:05,2015-02-12 23:59:05,incomplete,devrai
我不知道如何转换csv并将其保存到Excel工作表中。 请帮我解决这个问题。
答案 0 :(得分:2)
只需迭代rowItems
并使用createRow
/ createCell
无需csv。之后
sheet1.setColumnWidth(2, (15 * 500));
添加如下内容:
int row = 1;
for (final WorkItem item: rowItems) {
final Row row = sheet1.createRow(row);
int cell = 0;
Cell cell = row.createCell(cell++);
cell.setCellValue(item.getTaskId());
cell = row.createCell(cell++);
cell.setCellValue(item.getDAlias());
// ...
row += 1;
}