数据导出操作
Posted 是小叶的呢.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据导出操作相关的知识,希望对你有一定的参考价值。
效果图如下:
点击导出,把表中的数据导出在Excel工作表里面
html部分如下:
<div class="col-auto mt-2 mt-lg-0">
<button class="btn btn-outline-primary" type="button" onclick="exportExcel()">导出</button>
</div>
javascript部分如下:
function exportExcel() {
var formData = $("#addform").serialize();
window.open("/BaseInfor/Student/exportExcel?" + formData);
}
控制器部分如下:
public ActionResult exportExcel(int?collegeID,int?zhuanyeID,int?gradeID,int?classID,string search)
{
ReturnJson msg = new ReturnJson();
//查询要导出的数据
var list = from tbstudent in myModal.Studentchart
join tbcollege in myModal.Collegechart
on tbstudent.collegeID equals tbcollege.collegeID
join tbgrade in myModal.Gradechart
on tbstudent.gradeID equals tbgrade.gradeID
join tbzhuanye in myModal.Zhuanyechart
on tbstudent.zhuanyeID equals tbzhuanye.zhuanyeID
join tbclass in myModal.Classchart
on tbstudent.classID equals tbclass.classID
select new StudentVo
{
studentID = tbstudent.studentID,
studentName = tbstudent.studentName,
studentSex = tbstudent.studentSex,
studentCode = tbstudent.studentCode,
idCard = tbstudent.idCard,
photoName = tbstudent.photoName,
collegeID = tbstudent.collegeID,
collegeName = tbcollege.collegeName,
gradeID = tbgrade.gradeID,
gradeName = tbgrade.gradeName,
zhuanyeID = tbzhuanye.zhuanyeID,
zhuanyeName = tbzhuanye.zhuanyeName,
classID = tbclass.classID,
className = tbclass.className
};
//条件
if (collegeID != null && collegeID > 0)
{
list = list.Where(m => m.collegeID == collegeID);
}
if (gradeID != null && gradeID > 0)
{
list = list.Where(m => m.gradeID == gradeID);
}
if (zhuanyeID != null && zhuanyeID > 0)
{
list = list.Where(m => m.zhuanyeID == zhuanyeID);
}
if (classID != null && classID > 0)
{
list = list.Where(o => o.classID == classID);
}
//模糊搜索的条件
if (!string.IsNullOrEmpty(search))
{
//去空格
search = search.Trim();
//模糊查询
list = list.Where(o => o.studentName.Contains(search) || o.studentCode.Contains(search)
|| o.idCard.Contains(search));
}
List<StudentVo> liststudent = list.ToList();
//构建Excel
// 1、创建工作簿
NPOI.SS.UserModel.IWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
//2、创建工作表
NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("工作表名称");
workbook.SetSheetName(0, "学生信息");
// 3、创建标题行
NPOI.SS.UserModel.IRow rowTitle = sheet.CreateRow(0);//0行 索引 第一行
rowTitle.HeightInPoints = 35;//第一行的行高
//3-1创建单元格
NPOI.SS.UserModel.ICell cell0 = rowTitle.CreateCell(0);
//3-2设置单元格的值
cell0.SetCellValue("学生信息");
//列合并
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 8));
#region 单元格样式
//声明单元格的样式
NPOI.SS.UserModel.ICellStyle cellStyle_Title = workbook.CreateCellStyle();
//水平对其方式 center
cellStyle_Title.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
//垂直对齐方式
cellStyle_Title.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
//文字字体
NPOI.SS.UserModel.IFont font_title = workbook.CreateFont();
// 文字颜色
font_title.Color = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
//新版版本的字体加粗写法
font_title.IsBold = true;
cellStyle_Title.SetFont(font_title);
//设置边框的线
cellStyle_Title.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle_Title.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle_Title.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle_Title.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
cell0.CellStyle = cellStyle_Title;
#endregion
//4、创建表头行并设置字段
NPOI.SS.UserModel.IRow rowheader = sheet.CreateRow(1);
rowheader.CreateCell(0).SetCellValue("序号");
rowheader.CreateCell(1).SetCellValue("学号");
rowheader.CreateCell(2).SetCellValue("姓名");
rowheader.CreateCell(3).SetCellValue("身份证号");
rowheader.CreateCell(4).SetCellValue("性别");
rowheader.CreateCell(5).SetCellValue("学院");
rowheader.CreateCell(6).SetCellValue("专业");
rowheader.CreateCell(7).SetCellValue("年级");
rowheader.CreateCell(8).SetCellValue("班级");
#region 创建表头的样式
//声明样式
NPOI.SS.UserModel.ICellStyle cellStyle_header = workbook.CreateCellStyle();
cellStyle_header.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
cellStyle_header.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
//设置背景填充
cellStyle_header.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;
//设置背景填充颜色
cellStyle_header.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Aqua.Index;
//设置边框线为实线
cellStyle_header.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle_header.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle_header.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle_header.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
//设置字体
//声明字体
NPOI.SS.UserModel.IFont font_header = workbook.CreateFont();
//字体加粗
font_header.IsBold = true;
//字体大小
font_header.FontHeightInPoints = 10;
//加入单元格
cellStyle_header.SetFont(font_header);
// 给 rowHeader 单元格设置样式 循环
for (int i = 0; i < rowheader.Cells.Count; i++)
{
rowheader.GetCell(i).CellStyle = cellStyle_header;
}
#endregion
//5.遍历数据
for (int i = 0; i < liststudent.Count; i++)
{
//创建行
NPOI.SS.UserModel.IRow row = sheet.CreateRow(i+2);
row.HeightInPoints = 22;
row.CreateCell(0).SetCellValue(i + 1);
row.CreateCell(1).SetCellValue(liststudent[i].studentCode);
row.CreateCell(2).SetCellValue(liststudent[i].studentName);
row.CreateCell(3).SetCellValue(liststudent[i].idCard);
row.CreateCell(4).SetCellValue(liststudent[i].studentSex);
row.CreateCell(5).SetCellValue(liststudent[i].collegeName);
row.CreateCell(6).SetCellValue(liststudent[i].zhuanyeName);
row.CreateCell(7).SetCellValue(liststudent[i].gradeName);
row.CreateCell(8).SetCellValue(liststudent[i].className);
}
//6、设置列宽为自动适应
for(int i = 0; i < sheet.GetRow(1).Cells.Count; i++)
{
//列宽制动适应
sheet.AutoSizeColumn(i);
sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) * 12 / 10);
}
//把创建好的Excel输出到浏览器
string fileName = "学生信息" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".xls";
//把Excel转化为流输出
MemoryStream BookStream = new MemoryStream();//定义流
//将工作薄写入流
workbook.Write(BookStream);
//输出之前调用Seek 偏移量,游标位置
BookStream.Seek(0, SeekOrigin.Begin);
// 文件类型/文件名称
return File(BookStream, "application/vnd.ms-excel", fileName);
}
以上是关于数据导出操作的主要内容,如果未能解决你的问题,请参考以下文章