我创建了一个应用程序,我想将word文件,excel文件,图像,ppt等存储到数据库中。但是因为我是新手,我不知道如何存储这种文件。所以请帮助我
数据库类
public class DbHandler extends SQLiteOpenHelper {
private static final int dbVersion = 1;
private static final String dbName = "HSsuraksha";
private static final String tableName = "pocketDocs";
private static final String userId = "userId";
private static final String docId = "docId";
private static final String fileName = "fileName";
private static final String fileExt = "fileExt";
private static final String title = "title";
private static final String createTable = "CREATE TABLE " + tableName + "(" + userId + " Integer Primary Key," + docId + " Integer," + fileName + " Text," + fileExt + " Text," + title + " Text" + ")";
public DbHandler(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
}
public void insertData(DbModel dbModel) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
database.beginTransaction();
contentValues.put(userId, dbModel.userId);
contentValues.put(docId, dbModel.docId);
contentValues.put(fileName, dbModel.fileName);
contentValues.put(fileExt, dbModel.fileExtension);
contentValues.put(title, dbModel.title);
if (contentValues != null) {
Long id = database.insert(tableName, null, contentValues);
Log.e("insert values", "" + id);
}
database.endTransaction();
}
public DbModel selectDocs(String id) {
SQLiteDatabase db = this.getReadableDatabase();
DbModel model = new DbModel();
Cursor cursor = db.query(tableName, new String[]{fileName, fileExt, title}, docId + "=?", new String[]{id}, null, null, null, null);
if (cursor.moveToFirst()) {
model.fileName = cursor.getString(2);
model.fileExtension = cursor.getString(3);
model.title = cursor.getString(4);
}
db.close();
return model;
}
}
DbModel之后
public class DbModel implements Serializable {
public String userId, docId, fileName, fileExtension, title;
public DbModel() {
}
public DbModel(String userId, String docId, String fileName, String fileExtension, String title) {
this.userId = userId;
this.docId = docId;
this.fileName = fileName;
this.fileExtension = fileExtension;
this.title = title;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getDocId() {
return docId;
}
public void setDocId(String docId) {
this.docId = docId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileExtension() {
return fileExtension;
}
public void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
我要放数据的类
dbHandler = new DbHandler(context);
dbModel = new DbModel("1", "1", "GoogleDocs", ".pdf", "Test");
答案 0 :(得分:0)
我使用此lib进行excel导入和导出,支持最高ms2007
http://poi.apache.org/download.html
,轻松集成并使用
创建excel文件,其他文档引用poi lib文件。
// check if available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
Toast.makeText(context, "No External Storage", Toast.LENGTH_SHORT).show();
Log.w("FileUtils", "Storage not available or read only");
return false;
}
boolean success = false;
//New Workbook
Workbook wb = new HSSFWorkbook();
Cell c = null;
//Cell style for header row
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(HSSFColor.LIME.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//New Sheet
Sheet sheet1 = null;
sheet1 = wb.createSheet("job");
Row row = sheet1.createRow(0);
c = row.createCell(0);
c.setCellValue("OperatorId : ");
c.setCellStyle(cs);
c = row.createCell(1);
c.setCellValue("OperatorName : " );
c.setCellStyle(cs);
c = row.createCell(2);
c.setCellValue("Blade : " );
c.setCellStyle(cs);
c = row.createCell(3);
c.setCellValue("ShiftType : " );
c.setCellStyle(cs);
c = row.createCell(4);
c.setCellValue("Bom : ");
c.setCellStyle(cs);
c = row.createCell(5);
c.setCellValue("Job Created at : ");
c.setCellStyle(cs);
c = row.createCell(6);
c.setCellValue("Blade Number: " );
c.setCellStyle(cs);
sheet1.setColumnWidth(0, (15 * 500));
sheet1.setColumnWidth(1, (15 * 500));
sheet1.setColumnWidth(2, (15 * 500));
sheet1.setColumnWidth(3, (15 * 500));
sheet1.setColumnWidth(4, (15 * 500));
sheet1.setColumnWidth(5, (15 * 500));
sheet1.setColumnWidth(6, (15 * 500));
Row row2 = sheet1.createRow(1);
c = row2.createCell(0);
c.setCellValue("JobId");
c.setCellStyle(cs);
c = row2.createCell(1);
c.setCellValue("BladeName");
c.setCellStyle(cs);
c = row2.createCell(2);
c.setCellValue("Start Time");
c.setCellStyle(cs);
c = row2.createCell(3);
c.setCellValue("End Time");
c.setCellStyle(cs);
c = row2.createCell(4);
c.setCellValue("Time Taken");
c.setCellStyle(cs);
c = row2.createCell(5);
c.setCellValue("Ply Number");
c.setCellStyle(cs);
SimpleDateFormat sdf = new SimpleDateFormat("HHmm_ddMMyyyy");
String currentDateandTime = sdf.format(new Date());
File file = new File(context.getExternalFilesDir(null), historyID+"History_"+currentDateandTime+".xls");
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
wb.write(os);
Log.w("FileUtils", "Writing file" + file);
success = true;
} catch (IOException e) {
Log.w("FileUtils", "Error writing " + file, e);
} catch (Exception e) {
Log.w("FileUtils", "Failed to save file", e);
} finally {
try {
if (null != os)
os.close();
} catch (Exception ex) {
}
}
return success;
}
public static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
public static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
使用权限
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE
// Reading a excel file
File file = new File(context.getExternalFilesDir(null), filename);
FileInputStream myInput = new FileInputStream(file);
// Create a POIFSFileSystem object
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
// Create a workbook using the File System
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
// Get the first sheet from workbook
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
/** We now need something to iterate through the cells.**/
Iterator<Row> rowIter = mySheet.rowIterator();
Cell bladeIDcell = mySheet.getRow(0).getCell(0);
Cell bladeNameCell = mySheet.getRow(0).getCell(0);
HSSFSheet mySheet2 = myWorkBook.getSheetAt(1);
Iterator<Row> rowIter2 = mySheet2.rowIterator();
while(rowIter2.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter2.next();
Iterator<Cell> cellIter = myRow.cellIterator();
ArrayList<String> Valuesfromcell = new ArrayList<String>();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
Log.w("FileUtils", "Cell Value: " + myCell.toString());
Valuesfromcell.add(myCell.toString());
//Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
}