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

 我来答
百度网友9f653b7
2014-02-07 · TA获得超过104个赞
知道答主
回答量:69
采纳率:0%
帮助的人:79.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();
}
}
追问
CommonFunc这是什么类?
追答
只是我用来封装常用函数的一个类,fc.getPath()是用来获取当前项目文件路径的,你可以用常量替换
维特bP
2014-02-07 · TA获得超过406个赞
知道小有建树答主
回答量:659
采纳率:33%
帮助的人:182万
展开全部
上网查java mysql数据库接口和poi excel接口。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
狂_飆
2014-02-07 · TA获得超过175个赞
知道答主
回答量:153
采纳率:0%
帮助的人:99.8万
展开全部
用导入导出功能就行了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式