안드로이드 jxl.jar 엑셀 저장 (Excel save)
안드로이드에서 디비(sqllite etc)의 내용을 엑셀(xls)로 저장(save)을 하고자 할 때 사용하세요~
1) jxl.jar downlaod site
http://www.java2s.com/Code/Jar/j/jxl.htm
2) lib 폴더에 추가
3) 소스에 라이브러리 import
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
4) 내용 작성
/*
* 엑셀로 저장하기
*/
public synchronized void saveXls(CategoryItem hmUnit){
final CategoryItem cItem = hmUnit;
String sUnitFilePath = "";
File currentDB = null;
try {
File sd = new File(Environment.getExternalStorageDirectory(), "sampleData");
if (!sd.exists() && !sd.mkdirs()) {
Log.w(TAG, "Couldn't make dir " + sd);
return;
}else{
currentDB = new File(sd, hmUnit.getCtTitle()+".xls"); //저장폴더+저장파일(sample_save/sample_note_history.db)
sUnitFilePath = currentDB.getAbsolutePath();
}
} catch (Exception e) {
Log.i(TAG, ">saveXls : error : " + e.toString());
return;
}
final String sUrl = sUnitFilePath;
final File cDb = currentDB;
new AlertDialog.Builder(actMain).setIcon(
android.R.drawable.ic_dialog_alert)
.setTitle("Notice")
.setMessage("Save xls?\n\nfile path("+sUrl+")")
.setPositiveButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
;
}
}).setNegativeButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
try {
excelWrite(cDb, cItem);
Toast.makeText(actMain, "xls saved", Toast.LENGTH_SHORT).show();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).show();
}
public void excelWrite(File file, CategoryItem parmItem) throws WriteException, IOException {
// WorkBook 생성
WritableWorkbook wb = Workbook.createWorkbook(file);
// WorkSheet 생성
WritableSheet sh = wb.createSheet("Folder contents", 0);
// 열 넓이 설정(열 위치, 넓이)
sh.setColumnView(0, 10);
sh.setColumnView(1, 20);
sh.setColumnView(2, 20);
sh.setColumnView(3, 20);
// 셀형식
WritableCellFormat textFormat = new WritableCellFormat();
// 생성
//textFormat.setAlignment(Alignment.ALIGN_CENTER);
// 테두리
textFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
int row = 0;
// 헤더
Label label = new jxl.write.Label(0, row, "Enter Dt", textFormat);
sh.addCell(label);
label = new jxl.write.Label(1, row, "Update Dt", textFormat);
sh.addCell(label);
label = new jxl.write.Label(2, row, "Content", textFormat);
sh.addCell(label);
row++;
List<HistoryItem> arDdayInfo = null;
arDdayInfo = htManager.getHistoryItems(parmItem.getEnterTime(), "", "", "");
for(int i=0; i<arDdayInfo.size(); i++){
HistoryItem oneUnit = (HistoryItem)arDdayInfo.get(i);
// 등록 일자
label = new jxl.write.Label(0, row, (String) oneUnit.getDdayYmd(), textFormat);
sh.addCell(label);
// 업데이트 일자
label = new jxl.write.Label(1, row, (String) oneUnit.getAddinfo2(), textFormat);
sh.addCell(label);
// 내용
label = new jxl.write.Label(2, row, (String) oneUnit.getMemoCt(), textFormat);
sh.addCell(label);
row++;
}
// 워크시트 쓰기
wb.write();
// 워크시트 닫기
wb.close();
}
-----
Have a nice day~
댓글 없음:
댓글 쓰기