java怎样输出excel文件
答案:2 悬赏:50 手机版
解决时间 2021-04-14 01:33
- 提问者网友:謫仙
- 2021-04-13 01:00
java怎样输出excel文件
最佳答案
- 五星知识达人网友:佘樂
- 2021-04-13 02:24
//java生成简单的Excel文件
package beans.excel;
import java.io.IOException;
import java.io.OutputStream;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class SimpleExcelWrite {
public void createExcel(OutputStream os) throws WriteException,IOException{
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet",0);
//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
Label xuexiao = new Label(0,0,"学校");
sheet.addCell(xuexiao);
Label zhuanye = new Label(1,0,"专业");
sheet.addCell(zhuanye);
Label jingzhengli = new Label(2,0,"专业竞争力");
sheet.addCell(jingzhengli);
Label qinghua = new Label(0,1,"清华大学");
sheet.addCell(qinghua);
Label jisuanji = new Label(1,1,"计算机专业");
sheet.addCell(jisuanji);
Label gao = new Label(2,1,"高");
sheet.addCell(gao);
Label beida = new Label(0,2,"北京大学");
sheet.addCell(beida);
Label falv = new Label(1,2,"法律专业");
sheet.addCell(falv);
Label zhong = new Label(2,2,"中");
sheet.addCell(zhong);
Label ligong = new Label(0,3,"北京理工大学");
sheet.addCell(ligong);
Label hangkong = new Label(1,3,"航空专业");
sheet.addCell(hangkong);
Label di = new Label(2,3,"低");
sheet.addCell(di);
//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
}
}
package beans.excel;
import java.io.IOException;
import java.io.OutputStream;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class SimpleExcelWrite {
public void createExcel(OutputStream os) throws WriteException,IOException{
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet",0);
//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
Label xuexiao = new Label(0,0,"学校");
sheet.addCell(xuexiao);
Label zhuanye = new Label(1,0,"专业");
sheet.addCell(zhuanye);
Label jingzhengli = new Label(2,0,"专业竞争力");
sheet.addCell(jingzhengli);
Label qinghua = new Label(0,1,"清华大学");
sheet.addCell(qinghua);
Label jisuanji = new Label(1,1,"计算机专业");
sheet.addCell(jisuanji);
Label gao = new Label(2,1,"高");
sheet.addCell(gao);
Label beida = new Label(0,2,"北京大学");
sheet.addCell(beida);
Label falv = new Label(1,2,"法律专业");
sheet.addCell(falv);
Label zhong = new Label(2,2,"中");
sheet.addCell(zhong);
Label ligong = new Label(0,3,"北京理工大学");
sheet.addCell(ligong);
Label hangkong = new Label(1,3,"航空专业");
sheet.addCell(hangkong);
Label di = new Label(2,3,"低");
sheet.addCell(di);
//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
}
}
全部回答
- 1楼网友:醉吻情书
- 2021-04-13 02:59
import java.io.File;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Test {
public static void main(String[] args)
{
Test test = new Test();
test.exportExcel();
}
public void exportExcel() {
WritableWorkbook book = null;
try {
// 打开文件
book = Workbook.createWorkbook(new File("D:/测试.xls"));
// 生成名为"学生"的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("学生", 0);
// 指定单元格位置是第一列第一行(0, 0)以及单元格内容为张三
Label label = new Label(0, 0, "张三");
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
// 保存数字的单元格必须使用Number的完整包路径
jxl.write.Number number = new jxl.write.Number(1, 0, 30);
sheet.addCell(number);
// 写入数据并关闭文件
book.write();
} catch (Exception e) {
System.out.println(e);
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Test {
public static void main(String[] args)
{
Test test = new Test();
test.exportExcel();
}
public void exportExcel() {
WritableWorkbook book = null;
try {
// 打开文件
book = Workbook.createWorkbook(new File("D:/测试.xls"));
// 生成名为"学生"的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("学生", 0);
// 指定单元格位置是第一列第一行(0, 0)以及单元格内容为张三
Label label = new Label(0, 0, "张三");
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
// 保存数字的单元格必须使用Number的完整包路径
jxl.write.Number number = new jxl.write.Number(1, 0, 30);
sheet.addCell(number);
// 写入数据并关闭文件
book.write();
} catch (Exception e) {
System.out.println(e);
}finally{
if(book!=null){
try {
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯