使用java如何将mysql数据库表中数据写入到一张新excel相应列下?求具体的代码实现,我是新手。
3个回答
展开全部
下个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();
}
}
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();
}
}
追问
CommonFunc这是什么类?
追答
只是我用来封装常用函数的一个类,fc.getPath()是用来获取当前项目文件路径的,你可以用常量替换
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |