在android中,设计一个项目,允许用户上传一个指定格式的EXCELL表格,例如有姓名,单位两列。通过读取记录,把结果写入一个mysql表中,对应字段为name,unit请给出代码
时间: 2023-05-31 08:03:48 浏览: 108
以下是一个简单的Android项目示例,它允许用户上传一个指定格式的Excel表格,并将记录写入MySQL表中。
首先,需要在Android应用程序中添加以下依赖项:
```
implementation 'com.android.volley:volley:1.2.0'
implementation 'com.android.support:multidex:1.0.3'
implementation 'org.apache.poi:poi:4.1.2'
implementation 'org.apache.poi:poi-ooxml:4.1.2'
implementation 'mysql:mysql-connector-java:8.0.25'
```
然后,需要在Android应用程序中创建一个Activity,在该Activity中允许用户上传Excel文件。以下是一个简单的示例:
```
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
public class UploadActivity extends AppCompatActivity {
private static final String TAG = "UploadActivity";
private static final int REQUEST_CODE_PICK_FILE = 1;
private Button mUploadButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
mUploadButton = findViewById(R.id.upload_button);
mUploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_CODE_PICK_FILE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_FILE && resultCode == Activity.RESULT_OK && data != null) {
try {
Uri uri = data.getData();
String filePath = uri.getPath();
FileInputStream fileInputStream = new FileInputStream(new File(filePath));
Workbook workbook = WorkbookFactory.create(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Cell nameCell = row.getCell(0);
Cell unitCell = row.getCell(1);
String name = nameCell.getStringCellValue();
String unit = unitCell.getStringCellValue();
saveToMySQL(name, unit);
}
workbook.close();
fileInputStream.close();
Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e(TAG, "Error uploading file", e);
Toast.makeText(this, "上传失败", Toast.LENGTH_SHORT).show();
}
}
}
private void saveToMySQL(String name, String unit) {
// TODO: Add code to save to MySQL
}
}
```
在上面的示例中,我们定义了一个按钮,用于启动文件选择器。一旦用户选择了一个Excel文件,我们使用Apache POI库读取工作簿和工作表,并将每个单元格的值保存到MySQL中。
在保存到MySQL之前,需要先设置MySQL连接。以下是一个简单的示例,用于连接到MySQL数据库并执行查询:
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLHelper {
private static final String DB_HOST = "your-db-host";
private static final String DB_NAME = "your-db-name";
private static final String DB_USERNAME = "your-db-username";
private static final String DB_PASSWORD = "your-db-password";
public static Connection getConnection() throws SQLException {
String url = "jdbc:mysql://" + DB_HOST + "/" + DB_NAME;
return DriverManager.getConnection(url, DB_USERNAME, DB_PASSWORD);
}
public static void closeConnection(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closeStatement(Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closeResultSet(ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void executeQuery(String query) throws SQLException {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
} finally {
closeResultSet(resultSet);
closeStatement(statement);
closeConnection(connection);
}
}
}
```
现在,我们可以将saveToMySQL方法中的代码替换为以下代码,以将记录插入MySQL表中:
```
private void saveToMySQL(String name, String unit) {
try {
Connection connection = MySQLHelper.getConnection();
Statement statement = connection.createStatement();
String query = "INSERT INTO records (name, unit) VALUES ('" + name + "', '" + unit + "')";
statement.executeUpdate(query);
MySQLHelper.closeStatement(statement);
MySQLHelper.closeConnection(connection);
} catch (SQLException e) {
Log.e(TAG, "Error saving record to MySQL", e);
}
}
```
这将在MySQL表中插入每个记录,其中name和unit对应于Excel表格中的列。请确保将records替换为您的MySQL表名,并替换name和unit为您的列名。
此外,需要在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
现在,您的Android应用程序应该可以读取Excel文件并将记录插入MySQL表中。请注意,这只是一个简单的示例,可能需要根据您的应用程序的特定需求进行修改。
阅读全文