|
|
|
ขอคำแนะนำ Android Studio ในการสร้างไฟล์ Excel (XLS) บนมือถือ ด้วยครับ |
|
|
|
|
|
|
|
ลองดูตัวนี้นะครับ
http://www.andykhan.com/jexcelapi/
ให้โหลด jxl-2.6.12.jar มาใช้ครับ
Code (Android-Java)
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
String Fnamexls="testfile" + ".xls";
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/newfolder");
directory.mkdirs();
File file = new File(directory, Fnamexls);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook;
try {
int a = 1;
workbook = Workbook.createWorkbook(file, wbSettings);
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
Label label = new Label(0, 2, "SECOND");
Label label1 = new Label(0,1,"first");
Label label0 = new Label(0,0,"HEADING");
Label label3 = new Label(1,0,"Heading2");
Label label4 = new Label(1,1,String.valueOf(a));
try {
sheet.addCell(label);
sheet.addCell(label1);
sheet.addCell(label0);
sheet.addCell(label4);
sheet.addCell(label3);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
workbook.write();
try {
workbook.close();
} catch (WriteException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
|
|
|
|
Date :
2016-09-02 16:55:14 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อีกบทความครับ
Code (Android-Java)
/**
* Exports the cursor value to an excel sheet.
* Recommended to call this method in a separate thread,
* especially if you have more number of threads.
*
* @param cursor
*/
private void exportToExcel(Cursor cursor) {
final String fileName = "TodoList.xls";
//Saving file in external storage
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath() + "/javatechig.todo");
//create directory if not exist
if(!directory.isDirectory()){
directory.mkdirs();
}
//file path
File file = new File(directory, fileName);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook;
try {
workbook = Workbook.createWorkbook(file, wbSettings);
//Excel sheet name. 0 represents first sheet
WritableSheet sheet = workbook.createSheet("MyShoppingList", 0);
try {
sheet.addCell(new Label(0, 0, "Subject")); // column and row
sheet.addCell(new Label(1, 0, "Description"));
if (cursor.moveToFirst()) {
do {
String title = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TODO_SUBJECT));
String desc = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TODO_DESC));
int i = cursor.getPosition() + 1;
sheet.addCell(new Label(0, i, title));
sheet.addCell(new Label(1, i, desc));
} while (cursor.moveToNext());
}
//closing cursor
cursor.close();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
workbook.write();
try {
workbook.close();
} catch (WriteException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
http://jexcelapi.sourceforge.net/
http://sourceforge.net/projects/jexcelapi/files/
|
|
|
|
|
Date :
2016-09-02 16:59:10 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากครับ
ผมรบกวนถามอีกเรื่องครับ ผมสร้างไฟล์ excel ได้แล้ว แต่พอเสียบ usb ต่อกับ pc มันไม่เห็นไฟล์ที่เราสร้าง แต่ในมือถือมีไฟล์อยู่ เปิดดูได้ปกติ
เราสามารถแก้ไขได้มั้ยครับ
|
|
|
|
|
Date :
2016-09-03 12:42:28 |
By :
natchaphon_ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|