數據導出和數據導入剛好是相反的,把邏輯反過來就可以了。
源碼: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()方法就可以完成
?
? ?
?
?
?
?