easyui导出当前datagrid数据(含表头)
Posted 沐_沐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了easyui导出当前datagrid数据(含表头)相关的知识,希望对你有一定的参考价值。
JS代码
//导出当前DataGrid数据 function doExportCommon() { var list = getCheckedRowCommon(); var exportList = []; if (list.length <= 0) { showMessage("请至少勾选一条数据"); return; } var titleList = JSON.parse(getGridTitle()); exportList.push(titleList); for (var i = 0; i < list.length; i++) { var row = list[i]; var rowStr = "{"; //这里通过字段去匹配相应的行的数据 for (var field in titleList) { var str = row[field]; if (str) { while (str.toString().indexOf(‘\n‘) > 0) { str = str.replace(‘\n‘, "<br>"); } } else { str = ""; } rowStr += "\"" + field + "\":\"" + str + "\","; //过滤换行符 } rowStr = rowStr.substr(0, rowStr.length - 1); rowStr += "}"; exportList.push(JSON.parse(rowStr)); } var strhtml = JSON.stringify(exportList); //调用后台导出方法 callbackObj.showExportMsg(strhtml); } //该方法为自定义勾选框时,根据已勾选的勾选框获取行的数据 //获取勾选行,返回勾选行数据 function getCheckedRowCommon() { var data = $(‘#table‘).datagrid(‘getRows‘); var checkitems=$("#table_div input[name=‘checkItem‘]:checkbox:checked"); var rowList = []; for (var i = 0; i < checkitems.length; i++) { var rowTr = checkitems[i].parentElement.parentElement.parentElement; var selectIndex = $(rowTr).attr("datagrid-row-index"); rowList.push(data[selectIndex]); } return rowList; } //这里返回的是字段名称和表头文字的键值对
//根据datagrid显示的表头,获取文字 function getGridTitle() { var titlename = "{"; var fields = $(‘#table‘).datagrid(‘getColumnFields‘); for (var i = 0; i < fields.length; i++) { var option = $(‘#table‘).datagrid(‘getColumnOption‘, fields[i]); if (option.field != "checkItem" && option.hidden != true) { //过滤勾选框和隐藏列 titlename += "\"" + option.field + "\":\"" + option.title + "\","; } } titlename = titlename.substr(0, titlename.length - 1); titlename += "}"; return titlename; }
后台导出方法(winform)
这里应用到Aspose.Cells用来读写Excel
public class CallbackObjectForJs { public MainForm mainForm; //主页面 public void exportExcel(string data) { string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xlsx"; saveDialog.Filter = "Excel文件|*.xlsx"; saveDialog.FileName = DateTime.Now.Ticks.ToString(); saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; if (saveFileName.IndexOf(":") < 0) return; //被点了取消 Workbook wb = new Workbook(FileFormatType.Xlsx); try { Worksheet sheet = wb.Worksheets[0]; sheet.Name = "Sheet1"; // 为单元格添加样式 Aspose.Cells.Style style = wb.CreateStyle(); style.HorizontalAlignment = TextAlignmentType.Center; //设置居中 style.IsTextWrapped = true; //设置自动换行 //style.Font.Size = 12;//文字大小 //style.Font.IsBold = true;//粗体 DataTable dt = JsonConvert.DeserializeObject<DataTable>(data); //遍历DataTable for (int i = 0; i < dt.Rows.Count; i++) //行 { for (int j = 0; j < dt.Columns.Count; j++) { string value = dt.Rows[i][j].ToString(); while (value.Contains("<br>")) { value = value.Replace("<br>", "\n"); } sheet.Cells[i, j].PutValue(value); sheet.Cells[i, j].SetStyle(style); } } //设置宽度、高度自适应 setColumnWithAuto(sheet); wb.Save(saveFileName, new OoxmlSaveOptions(Aspose.Cells.SaveFormat.Xlsx)); //通过Invoke激活主线程,弹出框置顶。 mainForm.Invoke(new MethodInvoker(delegate { MessageBox.Show(mainForm, "导出成功"); })); } catch (Exception e) { MessageBox.Show(e.Message.ToString()); return; } } /// <summary> /// 设置表页的列宽度自适应 /// </summary> /// <param name="sheet">worksheet对象</param> public void setColumnWithAuto(Worksheet sheet) { Cells cells = sheet.Cells; int columnCount = cells.MaxColumn + 1; //获取表页的最大列数 int rowCount = cells.MaxRow; //获取表页的最大行数 sheet.AutoFitRows(); //设置行高自适应 for (int col = 0; col < columnCount; col++) { sheet.AutoFitColumn(col, 0, rowCount); } for (int col = 0; col < columnCount; col++) { cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30); } } }
以上是关于easyui导出当前datagrid数据(含表头)的主要内容,如果未能解决你的问题,请参考以下文章