excel easyui java_easyUI的datagrid中的数据导出成excel (Java语言)

首先引用 NPOI

代码语言c#

也不知道楼主用的什么语言  希望能对楼主有帮助

我不是大神  只是刚好照着例子做过导出excel  说的不完善 和有错误的地方要告诉我哦,不要让我再继续错下去了哦

public FileResult DaochuExcel()

{

//创建Excel文件的对象

NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();

//添加一个sheet

NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");

//获取list数据

IQueryable qs = null;

(这一段是查询数据)

List ListInfo = qs.ToList();

NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);

row1.Height = 3 * 265;

IFont cfont = book.CreateFont();  //自己设置的一个字体样式

cfont.FontName = "宋体";

cfont.FontHeight = 1 * 256;

row1.CreateCell(0).SetCellValue("订单号");   //自己设置的标题

row1.CreateCell(1).SetCellValue("订单状态");

sheet1.SetColumnWidth(0, 20*256);  //设置列宽

sheet1.SetColumnWidth(1, 15*256);

for (int i = 0; i < ListInfo.Count; i++)

{

NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);

rowtemp.Height = 3*265;

rowtemp.CreateCell(0).SetCellValue(ListInfo[i].order_number);

rowtemp.Cells[0].CellStyle.Alignment = HorizontalAlignment.Center;

rowtemp.Cells[0].CellStyle.VerticalAlignment = VerticalAlignment.Center;//这两句是垂直居中和水平居中

rowtemp.Cells[0].CellStyle.WrapText = true; //自动换行

rowtemp.Cells[0].CellStyle.GetFont(book).FontName = "宋体";

rowtemp.Cells[0].CellStyle.GetFont(book).FontHeight = 1 * 256;

rowtemp.CreateCell(1).SetCellValue(ListInfo[i].status);  //把值写到对应的格子里

rowtemp.CreateCell(2).SetCellValue(ListInfo[i].salesman);

}

Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";

// 写入到客户端

System.IO.MemoryStream ms = new System.IO.MemoryStream();

book.Write(ms);

ms.Seek(0, SeekOrigin.Begin);

ms.Flush();

ms.Position = 0;

return File(ms, "application/vnd.ms-excel", "文件名.xls");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值