java导出excel下载_Java导出,下载excel

本文展示了如何使用Java进行Excel文件的操作,包括读取Excel内容,创建并导出新的Excel文件,以及设置下载文件到本地。通过示例代码详细解释了每个步骤,包括使用特定库如Apache POI进行文件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java读取excel内容:

public class excel {

public static void main(String[] args) throws BiffException, IOException {

// TODO Auto-generated method stub

Workbook book = Workbook.getWorkbook(new File(

"C://Test.xls"));

Sheet sheet = book.getSheet(0);

List list = new ArrayList();

for (int k = 0; k 

for (int j = 0; j 

Cell cell = sheet.getCell(j, k);// 列,行

Cell keycell = sheet.getCell(j, 1);

String Content = cell.getContents();

list.add(Content);

}

}

for (String string : list) {

System.out.println(string);

}

book.close();

}

}

Java导出excel:

String filePath = "D:\\java\\myeclipse\\goodtaste_system"

+ "/upload/OrderRecord" + dateTime + ".xls";

filePath = request.getSession().getServletContext()

.getRealPath(ConstantFiled.UPLOAD)

+ "/OrderRecord" + dateTime + ".xls";

OutputStream os = new FileOutputStream(filePath);

book = Workbook.createWorkbook(os);

WritableSheet sheet = book.createSheet("订单", 0);

WritableFont wfont = new WritableFont(WritableFont.ARIAL, 11,

WritableFont.BOLD, false,

jxl.format.UnderlineStyle.NO_UNDERLINE,

jxl.format.Colour.BLACK);

WritableCellFormat titleFormat = new WritableCellFormat(wfont);

String[] title = { "订单号", "桌号", "点餐时间", "结账时间", "消费人数", "总价" };

for (int i = 0; i 

Label excelTitle = new Label(i, 0, title[i], titleFormat);

sheet.addCell(excelTitle);

}

List list = new ArrayList();

list = this.getOrderService()

.getAllOrdersByDateTime(dateTime + "%");

if (!list.isEmpty()) {

for (int i = 0; i 

sheet.addCell(new Label(0, i + 1, list.get(i).getOrderId()));

sheet.addCell(new Label(1, i + 1, list.get(i).getTableNum()));

sheet.addCell(new Label(2, i + 1, sdf.format(list.get(i)

.getOrderTime())));

sheet.addCell(new Label(3, i + 1, sdf.format(list.get(i)

.getAccountTime())));

sheet.addCell(new Label(4, i + 1, String.valueOf(list

.get(i).getConsmNums())));

sheet.addCell(new Label(5, i + 1, list.get(i)

.getTotalPrice()));

}

}

book.write();

Java下载文件:

String path = "D:\\java\\myeclipse\\goodtaste_system"

+ "/upload/OrderRecord" + dateTime + ".xls";

path = request.getSession().getServletContext()

.getRealPath(ConstantFiled.UPLOAD)

+ "/OrderRecord" + dateTime + ".xls";

File file = new File(path);

String filename = file.getName();

String ext = filename.substring(filename.lastIndexOf(".") + 1)

.toUpperCase();

InputStream fis = new BufferedInputStream(new FileInputStream(path));

byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();

response.reset();

response.addHeader("Content-Disposition", "attachment;filename="

+ new String(filename.getBytes()));

response.addHeader("Content-Length", "" + file.length());

OutputStream toClient = new BufferedOutputStream(

response.getOutputStream());

response.setContentType("application/octet-stream");

toClient.write(buffer);

toClient.flush();

toClient.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值