数据导出和数据导入刚好是相反的,把逻辑反过来就可以了。
源码:https://github.com/SeaLee02/FunctionModule/blob/master/UploadFiles/WebDemo/COM/DataToOut.aspx
效果:
然后勾选需要导出的数据,生成Excel
部分前台:
<div style="margin-left: 100px;"><asp:LinkButton CssClass="add" ID="lnkData" runat="server" OnClientClick="return Test()" OnClick="lnkData_Click">导出<br />数据</asp:LinkButton><asp:HiddenField ID="hfID" runat="server" /> //赋值选择了需要导出数据的ID</div>
JS:
//全选/不全选$("#checkAll").click(function () {var f = $("#checkAll").is(":checked")if (f) {$("input[name=chk]:checkbox").prop("checked", true);} else {$("input[name=chk]:checkbox").prop("checked", false);}});//单选var d = $("input[name=chk]:checkbox").length;$("input[name=chk]:checkbox").click(function () {var dd = $("input[name=chk]:checked").length;if (parseInt(d) === parseInt(dd)) {$("#checkAll").prop("checked", true);} else {$("#checkAll").prop("checked", false);}});//取值 function Test() { var ids = ""; var dd = $("input[name=chk]:checked"); dd.each(function () {ids += $(this).val() + ","; });if (ids=="") {alert("请选择导入的数据");return false;}ids = ids.substring(0, ids.length - 1);$("#hfID").val(ids);return true;};
后台导出数据的代码:
Aspose.Cells 需要从外面引入的
protected void lnkData_Click(object sender, EventArgs e){DataTable ds = new FunctionDemo.BLL.DataToLead().GetList(" ID in (" + hfID.Value + ")").Tables[0];if (ds.Rows.Count > 0){//创建一个excel表格Workbook workbook = new Workbook();Worksheet sheet = workbook.Worksheets[0] as Worksheet; //工作薄Cells cells = sheet.Cells;//取到所有的列//然后往cell里面插入数据 sheet.FreezePanes(1, 1, 1, 0);//冻结第一行
//插入头部cells[0, 0].PutValue("姓名");cells[0, 1].PutValue("性别");cells[0, 2].PutValue("年龄");cells[0, 3].PutValue("电话号码");cells[0, 4].PutValue("标记");Aspose.Cells.Style style1 = workbook.Styles[workbook.Styles.Add()]; //设置样式 然后进行设置style1.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;//居中//列宽cells.SetColumnWidth(0, 20);cells.SetColumnWidth(1, 20);cells.SetColumnWidth(2, 20);cells.SetColumnWidth(3, 50);cells.SetColumnWidth(4, 20);for (int i = 0; i < ds.Rows.Count; i++){cells.SetRowHeight(i, 20); //行高//赋值和样式cells[1 + i,0].PutValue(ds.Rows[i]["Name"].ToString());cells[1 + i,0].SetStyle(style1);
cells[1 + i,1].PutValue(ds.Rows[i]["Sex"].ToString());cells[1 + i,1].SetStyle(style1);
cells[1 + i,2].PutValue(ds.Rows[i]["Age"].ToString());cells[1 + i,2].SetStyle(style1);
cells[1 + i,3].PutValue(ds.Rows[i]["Tel"].ToString());cells[1 + i,3].SetStyle(style1);
cells[1 + i,4].PutValue(ds.Rows[i]["Remark"].ToString());cells[1 + i,4].SetStyle(style1);}Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()]; //设置样式style.HorizontalAlignment = TextAlignmentType.Center;//居中style.ForegroundColor = System.Drawing.Color.Khaki;//背景样式style.Pattern = BackgroundType.Solid; //枚举类型 模式 实线
cells[0,0].SetStyle(style); //第一行设计样式 头cells[0, 1].SetStyle(style); cells[0, 2].SetStyle(style); cells[0, 3].SetStyle(style); cells[0, 4].SetStyle(style);
System.IO.MemoryStream ms = workbook.SaveToStream();//生成流byte[] by = ms.ToArray();//生成字节好下载string fileName = "导出数据" + DateTime.Now.ToString("yyyyMMddHHmmss")+".xls";//文件名Response.ContentType = "application/octet-stream";//文件下载//以字节流形式下载excel 上面下面两种类型都可以//Response.ContentType = "application/vnd.ms-excel";//编码 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));Response.BinaryWrite(by);Response.Flush();Response.End();}}
合并单元格 使用 cells.Merge()方法就可以完成