前端 post 表单方式
function export_Interact() {
var url = "https://XXXXXXX:8083/sjjmeet/api/SignPerson/ExportListInteract?Token=123";
var form = $("<form>");//定义一个form表单
form.attr("style", "display:none");
form.attr("target", "");
form.attr("method", "post");//请求类型
form.attr("action", url);//请求地址
$("body").append(form);//将表单放置在web中
GetSerachData_Interact();
var input1 = $("<input>");
input1.attr("type", "hidden");
input1.attr("name", "orderField");
input1.attr("value", "CreateTime");
form.append(input1);
var input2 = $("<input>");
input2.attr("type", "hidden");
input2.attr("name", "orderType");
input2.attr("value", "desc");
form.append(input2);
form.submit();//表单提交
}
后台api
创建正常的api 控制器
[HttpPost]
public string ExportListTotal([FromBody] SignSearchModel model)
{
try
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from aaa where 1=1 ");
strSql.Append(GetUserType(model.UserType));
if (model != null)
{
if (!string.IsNullOrEmpty((model.StartTime)))
{
strSql.Append(" and g.BeginTime >= '" + model.StartTime + "'");
}
if (!string.IsNullOrEmpty((model.EndTime)))
{
strSql.Append(" and g.BeginTime <= '" + model.EndTime + "'");
}
}
strSql.Append(" group by p.UserID, p.UserName, p.FieldName, p.PTName, p.XiaowName order by FieldName");
//==以上 拷贝GetListInteract代码
var result = GetDataList<SignPersonTotal>(model, strSql);
DataTable AssetModelName = new DataTable();
AssetModelName.Columns.Add("领域");
AssetModelName.Columns.Add("序号");
AssetModelName.Columns.Add("平台");
AssetModelName.Columns.Add("小微");
AssetModelName.Columns.Add("姓名");
AssetModelName.Columns.Add("出席");
AssetModelName.Columns.Add("请假");
AssetModelName.Columns.Add("缺席");
foreach (var item in result)
{
DataRow dr = AssetModelName.NewRow();
dr["领域"] = item.FieldName;
dr["序号"] = item.Num;
dr["平台"] = item.PTName;
dr["小微"] = item.XiaowName;
dr["姓名"] = item.UserName;
dr["出席"] = item.Attend;
dr["请假"] = item.Leave;
dr["缺席"] = item.Absent;
AssetModelName.Rows.Add(dr);
}
Workbook workbook = new Workbook();
Worksheet cells = workbook.Worksheets[0];
Cells cellss = workbook.Worksheets[0].Cells;
Style titleStyle = workbook.CreateStyle();// workbook.Styles[workbook.Styles.Add()];//新增样式
titleStyle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
titleStyle.Font.Name = "微软雅黑";
titleStyle.Font.IsBold = true;
titleStyle.Font.Size = 11;
Style contentStyle = workbook.Styles[workbook.Styles.Add()];
contentStyle.HorizontalAlignment = TextAlignmentType.Center;
contentStyle.Font.Name = "微软雅黑";
//contentStyle.IsTextWrapped = true;//单元格内容自动换行
contentStyle.VerticalAlignment = TextAlignmentType.Center;
for (int i = 0; i < AssetModelName.Columns.Count; i++)
{
cells.Cells[0, i].PutValue(AssetModelName.Columns[i].ColumnName);
cells.Cells[0, i].SetStyle(titleStyle);
cells.Cells.SetColumnWidth(i, 20);
}
for (int i = 0; i < AssetModelName.Rows.Count; i++)
{
//合并领域,从第0列 第2行 开始
for (int j = 0; j < AssetModelName.Columns.Count; j++)
{
//mergeCells(a, b, c, d) 单元格合并函数
//第2行以后(不含表头),第0列的时候
if (i >= 1 && j == 0)
{
//判断 当前单元格领域是否和 上一行领域 0列相同,
var current = AssetModelName.Rows[i][j].ToString();
var up = AssetModelName.Rows[i - 1][j].ToString();
if (current == up)
{
//合并行。 列不变,行合并, c,d 不能为0,最小1. 合并两行 就是 c=2
cells.Cells.Merge(i, j, 2, 1);
cells.Cells[i + 1, j].SetStyle(contentStyle);
}
else
{
cells.Cells[i + 1, j].PutValue(AssetModelName.Rows[i][j].ToString());
cells.Cells[i + 1, j].SetStyle(contentStyle);
}
}
else
{
cells.Cells[i + 1, j].PutValue(AssetModelName.Rows[i][j].ToString());
cells.Cells[i + 1, j].SetStyle(contentStyle);
}
}
}
Stream st = workbook.SaveToStream();
st.Seek(0, SeekOrigin.Begin);
var contentDisposition = "";
switch (HttpContext.Current.Request.Browser.Browser)
{
case "IE":
case "Chrome":
contentDisposition = "出席统计" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx";
break;
case "Safari":
contentDisposition = "出席统计" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx";
break;
default:
contentDisposition = HttpUtility.UrlEncode("出席统计" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx");
break;
}
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
workbook.Save(response, contentDisposition, Aspose.Cells.ContentDisposition.Attachment, new Aspose.Cells.XlsSaveOptions(Aspose.Cells.SaveFormat.Xlsx));
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", contentDisposition);
HttpContext.Current.Response.AddHeader("Content-Length", st.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(workbook.SaveToStream().ToArray());
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
long chunkSize = 4096;
byte[] buffer = new byte[chunkSize];
long dataToRead = 0;
dataToRead = st.Length;
while (dataToRead > 0)
{
if (HttpContext.Current.Response.IsClientConnected)
{
int length = st.Read(buffer, 0, Convert.ToInt32(chunkSize));
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
HttpContext.Current.Response.Flush();
dataToRead -= length;
}
else
{
dataToRead = -1;
}
}
HttpContext.Current.Response.Close();
return "success";
}
catch (Exception e)
{
return e.Message;
}
}