1.首先申请读写SD卡权限
2.导入jxl.jar包 MainActivity
public class MainActivity extends Activity { Button btn; List<Order> orders = new ArrayList<Order>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //申请权限 verifyStoragePermissions(this); Order order = new Order("0001", "金太郎", "17766778899", "北京市朝阳区"); orders.add(order); btn = (Button) super.findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try { ExcelUtil.writeExcel(MainActivity.this, orders, "volume_" + new Date().toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } //申请读写权限 private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"}; public static void verifyStoragePermissions(Activity activity) { try { //检测是否有写的权限 int permission = ActivityCompat.checkSelfPermission(activity, "android.permission.WRITE_EXTERNAL_STORAGE"); if (permission != PackageManager.PERMISSION_GRANTED) { // 没有写的权限,去申请写的权限,会弹出对话框 ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE); } } catch (Exception e) { e.printStackTrace(); } } }
工具类
public class ExcelUtil { //内存地址 public static String root = Environment.getExternalStorageDirectory() .getPath(); public static void writeExcel(Context context, List<Order> exportOrder, String fileName) throws Exception { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && getAvailableStorage() > 1000000) { Toast.makeText(context, "SD卡不可用", Toast.LENGTH_LONG).show(); return; } String[] title = {"订单", "店名", "电话", "地址"}; File file; File dir = new File("/sdcard/Deye/"); file = new File(dir, fileName + ".xls"); if (!dir.exists()) { dir.mkdirs(); } // 创建Excel工作表 WritableWorkbook wwb; OutputStream os = new FileOutputStream(file); wwb = Workbook.createWorkbook(os); // 添加第一个工作表并设置第一个Sheet的名字 WritableSheet sheet = wwb.createSheet("订单", 0); Label label; for (int i = 0; i < title.length; i++) { // Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z // 在Label对象的子对象中指明单元格的位置和内容 label = new Label(i, 0, title[i], getHeader()); // 将定义好的单元格添加到工作表中 sheet.addCell(label); } for (int i = 0; i < exportOrder.size(); i++) { Order order = exportOrder.get(i); Label orderNum = new Label(0, i + 1, order.id); Label restaurant = new Label(1, i + 1, order.restName); Label nameLabel = new Label(2, i + 1, order.restPhone); Label address = new Label(3, i + 1, order.receiverAddr); sheet.addCell(orderNum); sheet.addCell(restaurant); sheet.addCell(nameLabel); sheet.addCell(address); Toast.makeText(context, "写入成功", Toast.LENGTH_LONG).show(); } // 写入数据 wwb.write(); // 关闭文件 wwb.close(); } public static WritableCellFormat getHeader() { WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD);// 定义字体 try { font.setColour(Colour.BLUE);// 蓝色字体 } catch (WriteException e1) { e1.printStackTrace(); } WritableCellFormat format = new WritableCellFormat(font); try { format.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中 format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中 // format.setBorder(Border.ALL, BorderLineStyle.THIN, // Colour.BLACK);// 黑色边框 // format.setBackground(Colour.YELLOW);// 黄色背景 } catch (WriteException e) { e.printStackTrace(); } return format; } /** * 获取SD可用容量 */ private static long getAvailableStorage() { StatFs statFs = new StatFs(root); long blockSize = statFs.getBlockSize(); long availableBlocks = statFs.getAvailableBlocks(); long availableSize = blockSize * availableBlocks; // Formatter.formatFileSize(context, availableSize); return availableSize; } }
Model类
public class Order implements Serializable { public String id; public String restPhone; public String restName; public String receiverAddr; public Order(String id, String restPhone, String restName, String receiverAddr) { this.id = id; this.restPhone = restPhone; this.restName = restName; this.receiverAddr = receiverAddr; } public Order(JSONObject obj) { this.id = obj.optString("order_number"); this.restPhone = obj.optString("Rphone"); this.restName = obj.optString("Rname"); this.receiverAddr = obj.optString("receiver_address"); } }导出按钮<Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="导出excel" /><string name="action_settings">Settings</string>
转载自:https://github.com/reallin/Android_Excel