Java后台创建Excel文档通过字节流发送,不生成实体文件(跳转)
java后台创建Excel实体文档:
直接运行下面代码即可(需引用jxl.jar包)
package com.gaohan.universal.test;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author gaohan
* @version 1.0
* @date 2020/6/3 16:09
*/
public class CreatExcel {
public static void main(String[] args) {
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream("D:/excel.xls");
createExcel(fileOut);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void createExcel(OutputStream os) throws WriteException, IOException {
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
Label region = new Label(0,0,"地区");
sheet.addCell(region);
Label supplier = new Label(1, 0, "供应商");
sheet.addCell(supplier);
Label commodity_code = new Label(2, 0, "商品编码");
sheet.addCell(commodity_code);
Label commodity_name = new Label(3, 0, "商品名称");
sheet.addCell(commodity_name);
Label operation_status = new Label(4, 0, "运营状态");
sheet.addCell(operation_status);
Label turnover_days = new Label(5, 0, "周转天数");
sheet.addCell(turnover_days);
Label purchase_quantity = new Label(6, 0, "建议采购量");
sheet.addCell(purchase_quantity);
Label a = new Label(0, 1, "test");
sheet.addCell(a);
Label b = new Label(1, 1, "test");
sheet.addCell(b);
Label c = new Label(2, 1, "test");
sheet.addCell(c);
Label d = new Label(3, 1, "test");
sheet.addCell(d);
Label e = new Label(4, 1, "test");
sheet.addCell(e);
Label f = new Label(5, 1, "test");
sheet.addCell(f);
Label g = new Label(6, 1, "test");
sheet.addCell(g);
//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
}
}
效果图如下: