1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| package com.contact;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import android.os.Environment;
import android.util.Log;
public class Filewriter {
public WritableWorkbook createWorkbook(String fileName){
//exports must use a temp file while writing to avoid memory hogging
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setUseTemporaryFileDuringWrite(true);
//get the sdcard's directory
File sdCard = Environment.getExternalStorageDirectory();
//add on the app's path
File dir = new File(sdCard.getAbsolutePath() + "/JExcelApiTest");
//make them in case they're not there
dir.mkdirs();
//create a standard java.io.File object for the Workbook to use
File wbfile = new File(dir,fileName);
WritableWorkbook wb = null;
try{
//create a new WritableWorkbook using the java.io.File and
//WorkbookSettings from above
wb = Workbook.createWorkbook(wbfile,wbSettings);
}catch(IOException ex){
Log.e("dd",ex.getStackTrace().toString());
}
return wb;
}
/**
*
* @param wb - WritableWorkbook to create new sheet in
* @param sheetName - name to be given to new sheet
* @param sheetIndex - position in sheet tabs at bottom of workbook
* @return - a new WritableSheet in given WritableWorkbook
*/
public WritableSheet createSheet(WritableWorkbook wb,
String sheetName, int sheetIndex){
//create a new WritableSheet and return it
return wb.createSheet(sheetName, sheetIndex);
}
/**
*
* @param columnPosition - column to place new cell in
* @param rowPosition - row to place new cell in
* @param contents - string value to place in cell
* @param headerCell - whether to give this cell special formatting
* @param sheet - WritableSheet to place cell in
* @throws RowsExceededException - thrown if adding cell exceeds .xls row limit
* @throws WriteException - Idunno, might be thrown
*/
public void writeCell(int columnPosition, int rowPosition, String contents, boolean headerCell,
WritableSheet sheet) throws RowsExceededException, WriteException{
//create a new cell with contents at position
Label newCell = new Label(columnPosition,rowPosition,contents);
if (headerCell){
//give header cells size 10 Arial bolded
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
//center align the cells' contents
headerFormat.setAlignment(Alignment.CENTRE);
newCell.setCellFormat(headerFormat);
}
sheet.addCell(newCell);
}
} |
Partager