这是原demo的网址https://blog.csdn.net/zhangphil/article/details/86083376?utm_medium=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-2.test_version_3&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-2.test_version_,我按照原demo copy到了我的studio,但是出来的xls文件打开了乱码很卡无法编辑不知道哪里出了问题?
下面是我的code:
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MySQLiteOpenHelper extends SQLiteOpenHelper{
//数据库名称。
public static final String DATABASE_NAME = "zhangphil.db";
//数据库版本号。
public static int DATABASE_VERSION = 1;
private static MySQLiteOpenHelper helper;
//表名。
public static final String TABLE_NAME = "Student";
public static final String STUDENT_ID = "id";
public static final String STUDENT_NAME = "name";
public static final String STUDENT_GENDER = "gender";
public static final String STUDENT_AGE = "age";
//创建数据库表的SQL语句。
private String sql_create_table = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + STUDENT_ID + " integer primary key autoincrement," + STUDENT_NAME + " varchar(60)," + STUDENT_GENDER + " varchar(1)," + STUDENT_AGE + " int)";
public static MySQLiteOpenHelper getInstance(Context context) {
if (helper == null) {
helper = new MySQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
return helper;
}
public MySQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库的表,如果不存在。
db.execSQL(sql_create_table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase sqLiteDatabase = MySQLiteOpenHelper.getInstance(this).getWritableDatabase();
ContentValues contentValues1 = getContentValues("zhang", "男", 18);
ContentValues contentValues2 = getContentValues("phil", "男", 19);
//往SQLite数据库中插入两条数据。
sqLiteDatabase.insert(MySQLiteOpenHelper.TABLE_NAME, null, contentValues1);
sqLiteDatabase.insert(MySQLiteOpenHelper.TABLE_NAME, null, contentValues2);
sqLiteDatabase.close();
//从SQLite数据库中读出数据。
List<Student> students = query(MySQLiteOpenHelper.getInstance(this).getReadableDatabase());
HSSFWorkbook mWorkbook = new HSSFWorkbook();
HSSFSheet mSheet = mWorkbook.createSheet(MySQLiteOpenHelper.TABLE_NAME);
createExcelHead(mSheet);
for (Student student : students) {
//System.out.println(student.id + "," + student.name + "," + student.gender + "," + student.age);
createCell(student.id, student.name, student.gender, student.age, mSheet);
}
File xlsFile = new File(Environment.getExternalStorageDirectory(), "excel.xls");
try {
if (!xlsFile.exists()) {
xlsFile.createNewFile();
}
mWorkbook.write(xlsFile);// 或者以流的形式写入文件 mWorkbook.write(new FileOutputStream(xlsFile));
mWorkbook.close();
Toast.makeText(MainActivity.this,"excel create success!",Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
private ContentValues getContentValues(String name, String gender, int age) {
ContentValues contentValues = new ContentValues();
contentValues.put(MySQLiteOpenHelper.STUDENT_NAME, name);
contentValues.put(MySQLiteOpenHelper.STUDENT_GENDER, gender);
contentValues.put(MySQLiteOpenHelper.STUDENT_AGE, age);
return contentValues;
}
//查询SQLite数据库。读出所有数据内容。
@SuppressLint("Range")
private List<Student> query(SQLiteDatabase db) {
List<Student> students = null;
Cursor cursor = db.rawQuery("SELECT * FROM " + MySQLiteOpenHelper.TABLE_NAME, null);
if (cursor != null && cursor.getCount() > 0) {
students = new ArrayList<>();
while (cursor.moveToNext()) {
Student student = new Student();
student.id = cursor.getInt(cursor.getColumnIndex(MySQLiteOpenHelper.STUDENT_ID));
student.name = cursor.getString(cursor.getColumnIndex(MySQLiteOpenHelper.STUDENT_NAME));
student.gender = cursor.getString(cursor.getColumnIndex(MySQLiteOpenHelper.STUDENT_GENDER));
student.age = cursor.getInt(cursor.getColumnIndex(MySQLiteOpenHelper.STUDENT_AGE));
students.add(student);
}
cursor.close();
}
db.close();
return students;
}
//数据容器,装载从数据库中读出的数据内容。
private class Student {
public int id;
public String name;
public String gender;
public int age;
}
// 创建Excel标题行,第一行。
private void createExcelHead(HSSFSheet mSheet) {
HSSFRow headRow = mSheet.createRow(0);
headRow.createCell(0).setCellValue(MySQLiteOpenHelper.STUDENT_ID);
headRow.createCell(1).setCellValue(MySQLiteOpenHelper.STUDENT_NAME);
headRow.createCell(2).setCellValue(MySQLiteOpenHelper.STUDENT_GENDER);
headRow.createCell(3).setCellValue(MySQLiteOpenHelper.STUDENT_AGE);
}
// 创建Excel的一行数据。
private static void createCell(int id, String name, String gender, int age, HSSFSheet sheet) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
dataRow.createCell(0).setCellValue(id);
dataRow.createCell(1).setCellValue(name);
dataRow.createCell(2).setCellValue(gender);
dataRow.createCell(3).setCellValue(age);
}
}