使用java如何将mysql数据库表中数据写入到一张新excel相应列下?求具体的代码实现,我是新手。

2024-12-04 20:51:00
推荐回答(3个)
回答1:

下个jxl包,直接给代码吧,读写删都有,不懂再问我
import jxl.JXLException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExcelService {
private String path = null;
private String sheetname = null;

public ExcelService(String FilePath, String SheetName) {
CommonFunc fc = new CommonFunc();
path = fc.getPath() + FilePath;
sheetname = SheetName;
}

public String read(int colum, int row) throws Exception {
FileInputStream fileInputStream = new FileInputStream(path);
Workbook workbook = Workbook.getWorkbook(fileInputStream);
Sheet sheet = workbook.getSheet(sheetname);
Cell cell = sheet.getCell(colum, row);
String content = cell.getContents();
workbook.close();
fileInputStream.close();
return content;
}

public void write(String str, int colum, int row) throws Exception {
File file = new File(path);
Workbook workbook = Workbook.getWorkbook(file);
WritableWorkbook book = workbook.createWorkbook(file, workbook);
WritableSheet sheet = book.getSheet(sheetname);
sheet.addCell(new Label(colum, row, str));
book.write();
book.close();
}

public int getRowCount() throws Exception {
FileInputStream fileInputStream = new FileInputStream(path);
Workbook workbook = Workbook.getWorkbook(fileInputStream);
Sheet sheet = workbook.getSheet(sheetname);
int count = sheet.getRows();
workbook.close();
fileInputStream.close();
return count;
}

public int getClumnCount() throws Exception {
FileInputStream fileInputStream = new FileInputStream(path);
Workbook workbook = Workbook.getWorkbook(fileInputStream);
Sheet sheet = workbook.getSheet(sheetname);
int count = sheet.getColumns();
workbook.close();
fileInputStream.close();
return count;
}
public void deleteFirstLine(int row) throws Exception{
File file = new File(path);
Workbook workbook = Workbook.getWorkbook(file);
WritableWorkbook book = Workbook.createWorkbook(file,workbook);
WritableSheet sheet = book.getSheet(sheetname);
sheet.removeRow(row);
book.write();
book.close();
workbook.close();
}
}

回答2:

上网查java mysql数据库接口和poi excel接口。

回答3:

用导入导出功能就行了