Java 我的所有Sqlitedatabase行都转换为一个简单的excel
到目前为止,我已经成功地创建了一个excel文件,并从sqlite.db中保存了一些数据 问题:如何将SQLITE数据库中的所有行保存到excel文件中?有什么建议吗 新手谢谢示例代码?一直遵循Sqlite指南 擅长我只知道如何从数据库中获取第一批数据,但我不知道如何获取所有数据?在这里使用游标是最好的方法吗 db.querySELECT*从MYTABLE中我想获取所有行和行中的所有数据,然后显示在excel文档中。c、 setCellValueitem_id 仅显示特定行中的信息该行中的所有行Java 我的所有Sqlitedatabase行都转换为一个简单的excel,java,android,eclipse,excel,sqlite,Java,Android,Eclipse,Excel,Sqlite,到目前为止,我已经成功地创建了一个excel文件,并从sqlite.db中保存了一些数据 问题:如何将SQLITE数据库中的所有行保存到excel文件中?有什么建议吗 新手谢谢示例代码?一直遵循Sqlite指南 擅长我只知道如何从数据库中获取第一批数据,但我不知道如何获取所有数据?在这里使用游标是最好的方法吗 db.querySELECT*从MYTABLE中我想获取所有行和行中的所有数据,然后显示在excel文档中。c、 setCellValueitem_id 仅显示特定行中的信息该行中的所有行
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
final int item_id = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.KEY_ID));
String getREG = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_REGNR));
String getMATAR = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_MIL));
String getDATE = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_DATUMNOW));
Workbook wb = new HSSFWorkbook();
Cell c = null;
CellStyle cs = wb.createCellStyle();
CellStyle cs_row = wb.createCellStyle();
cs_row.setFillPattern(HSSFCellStyle.ALIGN_LEFT);
Sheet sheet1 = null;
sheet1 = wb.createSheet("Tankningsrapport");
// Generate column headings
Row row = sheet1.createRow(0);
Row row1 = sheet1.createRow(1);
c = row.createCell(0);
c.setCellValue("Tankning");
c.setCellStyle(cs);
c = row.createCell(1);
c.setCellValue("Datum");
c.setCellStyle(cs);
c = row.createCell(2);
c.setCellValue("REGNR");
c.setCellStyle(cs);
c = row1.createCell(0);
c.setCellValue(item_id);
c.setCellStyle(cs_row);
c = row1.createCell(1);
c.setCellValue(getDATE);
c.setCellStyle(cs_row);
c = row1.createCell(2);
c.setCellValue(getREG);
c.setCellStyle(cs_row);
sheet1.setColumnWidth(0, (15 * 500));
sheet1.setColumnWidth(1, (15 * 500));
sheet1.setColumnWidth(2, (15 * 500));
你可以试试这个它对我有用
SQLiteAdapter helper = new SQLiteAdapter(visaRapporter.this);
helper.openToWrite();
ArrayList<String> lis = new ArrayList<String>();
Cursor cursor = mySQLiteAdapter.queueAll();
//New Workbook
Workbook wb = new HSSFWorkbook();
CellStyle cs_row = wb.createCellStyle();
cs_row.setFillPattern(HSSFCellStyle.ALIGN_LEFT);
//New Sheet
HSSFSheet sheet1 = null;
sheet1 = (HSSFSheet) wb.createSheet("Tankningsrapport");
// Generate column headings
HSSFRow rowHead = sheet1.createRow((short) 0);
// Head rows
rowHead.createCell((short) 0).setCellValue("Registerings nummer");
rowHead.createCell((short) 1).setCellValue("Mätarinställning i km");
rowHead.createCell((short) 2).setCellValue("Datum");
rowHead.createCell((short) 3).setCellValue("Månad");
rowHead.createCell((short) 4).setCellValue("Tankad mängd");
rowHead.createCell((short) 5).setCellValue("Tankställe");
rowHead.createCell((short) 6).setCellValue("Signatur");
rowHead.createCell((short) 7).setCellValue("Kontroll Olja: ");
rowHead.createCell((short) 8).setCellValue("Ringtryck VBG: ");
rowHead.createCell((short) 9).setCellValue("Ringtryck bil:");
rowHead.createCell((short) 10).setCellValue("Ringtryck trailer:");
int i = 1;
while (cursor.moveToNext()) {
HSSFRow rowHead1 = sheet1.createRow((short) i);
rowHead1.createCell((short) 0).setCellValue(cursor.getString(1));
rowHead1.createCell((short) 1).setCellValue(cursor.getString(2));
rowHead1.createCell((short) 2).setCellValue(cursor.getString(3));
rowHead1.createCell((short) 3).setCellValue(cursor.getString(4));
rowHead1.createCell((short) 4).setCellValue(cursor.getString(5));
rowHead1.createCell((short) 5).setCellValue(cursor.getString(6));
rowHead1.createCell((short) 6).setCellValue(cursor.getString(7));
rowHead1.createCell((short) 7).setCellValue(cursor.getString(8));
rowHead1.createCell((short) 8).setCellValue(cursor.getString(9));
rowHead1.createCell((short) 9).setCellValue(cursor.getString(10));
rowHead1.createCell((short) 10).setCellValue(cursor.getString(11));
lis.add(cursor.getString(2));
i++;
}
for (int j = 0; j <= lis.size() - 1; j++) {
Cursor cursor2 = mySQLiteAdapter.query("select * from Tanking1 where Datum ='" + lis.get(j) + "'");
while (cursor.moveToNext()) {
sheet1 = (HSSFSheet) wb.createSheet("Tankningsrapport");
HSSFRow rowhead1 = sheet1.createRow((short) 0);
rowhead1.createCell((short) 0).setCellValue("Registerings nummer");
rowhead1.createCell((short) 1).setCellValue("Mätarinställning i km");
rowhead1.createCell((short) 2).setCellValue("Datum");
rowhead1.createCell((short) 3).setCellValue("Månad");
rowhead1.createCell((short) 4).setCellValue("Tankad mängd");
rowhead1.createCell((short) 5).setCellValue("Tankställe");
rowhead1.createCell((short) 6).setCellValue("Signatur");
rowhead1.createCell((short) 7).setCellValue("Kontroll Olja: ");
rowhead1.createCell((short) 8).setCellValue("Ringtryck VBG: ");
rowhead1.createCell((short) 9).setCellValue("Ringtryck bil:");
rowhead1.createCell((short) 10).setCellValue("Ringtryck trailer:");
HSSFRow rowlow = sheet1.createRow((short) 1);
rowlow.createCell((short) 0).setCellValue(cursor2.getString(1));
rowlow.createCell((short) 1).setCellValue(cursor2.getString(2));
rowlow.createCell((short) 2).setCellValue(cursor2.getString(3));
rowlow.createCell((short) 3).setCellValue(cursor2.getString(4));
rowlow.createCell((short) 4).setCellValue(cursor2.getString(5));
rowlow.createCell((short) 5).setCellValue(cursor2.getString(6));
rowlow.createCell((short) 6).setCellValue(cursor2.getString(7));
rowlow.createCell((short) 7).setCellValue(cursor2.getString(8));
rowlow.createCell((short) 8).setCellValue(cursor2.getString(9));
rowlow.createCell((short) 9).setCellValue(cursor2.getString(10));
rowlow.createCell((short) 10).setCellValue(cursor2.getString(11));
}
sheet1.setColumnWidth(0, (10 * 250));
sheet1.setColumnWidth(1, (10 * 400));
sheet1.setColumnWidth(2, (10 * 400));
sheet1.setColumnWidth(3, (10 * 400));
sheet1.setColumnWidth(4, (10 * 400));
sheet1.setColumnWidth(5, (10 * 400));
sheet1.setColumnWidth(6, (10 * 400));
sheet1.setColumnWidth(7, (10 * 350));
sheet1.setColumnWidth(8, (10 * 350));
sheet1.setColumnWidth(9, (10 * 350));
sheet1.setColumnWidth(10, (10 * 350));
// Sparar ner till fil.
File file = new File(context.getExternalFilesDir(null), fileName);
FileOutputStream os = null;
Toast.makeText(visaRapporter.this,
"Filen är nu sparad och omgjord från databasen till Excel.", Toast.LENGTH_SHORT).show();
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
wb.write(os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.w("FileUtils", "Lägger till xls filen" + file);
}
}
在for/while列表中,我计算数据库中的每一行,并将其放入.xls文件的新行中 我将它们保存到CSV文件中,以分号分隔。默认情况下,Excel会接受这一点,这样我就不会在应用程序中放置另一个第三方库。问题不在于如何保存文件。我只是想知道如何将我所有的数据保存到excel文件中。我给了你一个替代建议。谢谢你的Vyger,但这对我现在没有帮助:也许4周前我会非常高兴。我这么做,是为了将数据从dbs共享到Microsoft world。。。我尽可能使用最少的第三方库来保持应用程序的小型化和性能。
SQLiteAdapter helper = new SQLiteAdapter(visaRapporter.this);
helper.openToWrite();
ArrayList<String> lis = new ArrayList<String>();
Cursor cursor = mySQLiteAdapter.queueAll();
//New Workbook
Workbook wb = new HSSFWorkbook();
CellStyle cs_row = wb.createCellStyle();
cs_row.setFillPattern(HSSFCellStyle.ALIGN_LEFT);
//New Sheet
HSSFSheet sheet1 = null;
sheet1 = (HSSFSheet) wb.createSheet("Tankningsrapport");
// Generate column headings
HSSFRow rowHead = sheet1.createRow((short) 0);
// Head rows
rowHead.createCell((short) 0).setCellValue("Registerings nummer");
rowHead.createCell((short) 1).setCellValue("Mätarinställning i km");
rowHead.createCell((short) 2).setCellValue("Datum");
rowHead.createCell((short) 3).setCellValue("Månad");
rowHead.createCell((short) 4).setCellValue("Tankad mängd");
rowHead.createCell((short) 5).setCellValue("Tankställe");
rowHead.createCell((short) 6).setCellValue("Signatur");
rowHead.createCell((short) 7).setCellValue("Kontroll Olja: ");
rowHead.createCell((short) 8).setCellValue("Ringtryck VBG: ");
rowHead.createCell((short) 9).setCellValue("Ringtryck bil:");
rowHead.createCell((short) 10).setCellValue("Ringtryck trailer:");
int i = 1;
while (cursor.moveToNext()) {
HSSFRow rowHead1 = sheet1.createRow((short) i);
rowHead1.createCell((short) 0).setCellValue(cursor.getString(1));
rowHead1.createCell((short) 1).setCellValue(cursor.getString(2));
rowHead1.createCell((short) 2).setCellValue(cursor.getString(3));
rowHead1.createCell((short) 3).setCellValue(cursor.getString(4));
rowHead1.createCell((short) 4).setCellValue(cursor.getString(5));
rowHead1.createCell((short) 5).setCellValue(cursor.getString(6));
rowHead1.createCell((short) 6).setCellValue(cursor.getString(7));
rowHead1.createCell((short) 7).setCellValue(cursor.getString(8));
rowHead1.createCell((short) 8).setCellValue(cursor.getString(9));
rowHead1.createCell((short) 9).setCellValue(cursor.getString(10));
rowHead1.createCell((short) 10).setCellValue(cursor.getString(11));
lis.add(cursor.getString(2));
i++;
}
for (int j = 0; j <= lis.size() - 1; j++) {
Cursor cursor2 = mySQLiteAdapter.query("select * from Tanking1 where Datum ='" + lis.get(j) + "'");
while (cursor.moveToNext()) {
sheet1 = (HSSFSheet) wb.createSheet("Tankningsrapport");
HSSFRow rowhead1 = sheet1.createRow((short) 0);
rowhead1.createCell((short) 0).setCellValue("Registerings nummer");
rowhead1.createCell((short) 1).setCellValue("Mätarinställning i km");
rowhead1.createCell((short) 2).setCellValue("Datum");
rowhead1.createCell((short) 3).setCellValue("Månad");
rowhead1.createCell((short) 4).setCellValue("Tankad mängd");
rowhead1.createCell((short) 5).setCellValue("Tankställe");
rowhead1.createCell((short) 6).setCellValue("Signatur");
rowhead1.createCell((short) 7).setCellValue("Kontroll Olja: ");
rowhead1.createCell((short) 8).setCellValue("Ringtryck VBG: ");
rowhead1.createCell((short) 9).setCellValue("Ringtryck bil:");
rowhead1.createCell((short) 10).setCellValue("Ringtryck trailer:");
HSSFRow rowlow = sheet1.createRow((short) 1);
rowlow.createCell((short) 0).setCellValue(cursor2.getString(1));
rowlow.createCell((short) 1).setCellValue(cursor2.getString(2));
rowlow.createCell((short) 2).setCellValue(cursor2.getString(3));
rowlow.createCell((short) 3).setCellValue(cursor2.getString(4));
rowlow.createCell((short) 4).setCellValue(cursor2.getString(5));
rowlow.createCell((short) 5).setCellValue(cursor2.getString(6));
rowlow.createCell((short) 6).setCellValue(cursor2.getString(7));
rowlow.createCell((short) 7).setCellValue(cursor2.getString(8));
rowlow.createCell((short) 8).setCellValue(cursor2.getString(9));
rowlow.createCell((short) 9).setCellValue(cursor2.getString(10));
rowlow.createCell((short) 10).setCellValue(cursor2.getString(11));
}
sheet1.setColumnWidth(0, (10 * 250));
sheet1.setColumnWidth(1, (10 * 400));
sheet1.setColumnWidth(2, (10 * 400));
sheet1.setColumnWidth(3, (10 * 400));
sheet1.setColumnWidth(4, (10 * 400));
sheet1.setColumnWidth(5, (10 * 400));
sheet1.setColumnWidth(6, (10 * 400));
sheet1.setColumnWidth(7, (10 * 350));
sheet1.setColumnWidth(8, (10 * 350));
sheet1.setColumnWidth(9, (10 * 350));
sheet1.setColumnWidth(10, (10 * 350));
// Sparar ner till fil.
File file = new File(context.getExternalFilesDir(null), fileName);
FileOutputStream os = null;
Toast.makeText(visaRapporter.this,
"Filen är nu sparad och omgjord från databasen till Excel.", Toast.LENGTH_SHORT).show();
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
wb.write(os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.w("FileUtils", "Lägger till xls filen" + file);
}
}