前后端分离,get请求导出

[HttpGet]
public HttpResponseMessage Export(string obj)
{
string eventType = string.Empty;
string exportFileName = string.Empty;

//查询出要导出数据json字符串
var resultValue = Query(obj);

//解析join字符串到对象
var result = JsonConvert.DeserializeObject<Data>(resultValue);

//解析join字符串到对象
//JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
//javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
//var result = javaScriptSerializer.Deserialize<Data>(resultValue);

if (result.Data.PageDatas.Count < 50000)
{
var list = result.Data.PageDatas;
var res = ExcelImport(list, eventType.ToString(), exportFileName);
return res;
}
else
{
var message = "数据太多,无法导出数据";
string str = ApiResultHelper.GetMessage(message.ToString());
return new HttpResponseMessage { Content = new StringContent(str, Encoding.UTF8, "application/json") };
//return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}

protected HttpResponseMessage ExcelImport(List<PageData> PageDatas)
{
DataTable dt = new DataTable();

#region DataTable数据

dt.Columns.Add("事件名称");
dt.Columns.Add("设备名称");
dt.Columns.Add("事件类型");
dt.Columns.Add("发生时间");

int rowIndex = 0;
foreach (var item in PageDatas)
{
dt.Rows.Add();
dt.Rows[rowIndex][0] = item.EventName;
dt.Rows[rowIndex][1] = item.DeviceName;
dt.Rows[rowIndex][2] = item.RecordName;
dt.Rows[rowIndex][3] = item.IssueTime;
rowIndex++;
}

#endregion DataTable数据

var file = ExcelStream(dt);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(file);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = exportFileName + ".xls";
return result;
}


private MemoryStream ExcelStream(DataTable dt)
{
//var list = dc.v_bs_dj_bbcdd1.Where(eps).ToList();
HSSFWorkbook workbook = new HSSFWorkbook();

ISheet sheet1 = workbook.CreateSheet("Sheet1");

//1-创建首行
IRow headRow = sheet1.CreateRow(0);
//1.1-设置首行样式
IFont font = workbook.CreateFont();
font.Boldweight = (short)FontBoldWeight.Bold; //字体加粗
ICellStyle headCellStyle = workbook.CreateCellStyle();
headCellStyle.SetFont(font);
headCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
//1.2-设置首行标题
int rowIndex = 0;
foreach (DataColumn dc in dt.Columns)
{
ICell cell = headRow.CreateCell(rowIndex);
cell.SetCellValue(dc.ColumnName);
cell.CellStyle = headCellStyle;
rowIndex++;
}

//2.1-内容行样式
ICellStyle contentCellStyle = workbook.CreateCellStyle();
contentCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
contentCellStyle.WrapText = true; //自动换行

int rowCount = 0;
if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
IRow row = sheet1.CreateRow(rowCount + 1);
for (int i = 0; i < dt.Columns.Count; i++)
{
ICell cell = row.CreateCell(i);
cell.SetCellValue(dr[i].ToString());
cell.CellStyle = contentCellStyle;
}
rowCount++;
}
}

for (var i = 1; i < dt.Rows.Count; i++)
{
IRow row = sheet1.CreateRow(i + 1);
//设置内容
for (var j = 0; j < dt.Columns.Count; j++)
{
ICell cell = row.CreateCell(j);
cell.SetCellValue(dt.Rows[i][j].ToString());
cell.CellStyle = contentCellStyle;
}
}
//3-自动列宽-根据内容长度自动展开
for (int i = 0; i < dt.Columns.Count; i++)
{
sheet1.AutoSizeColumn(i);
}

MemoryStream file = new MemoryStream();
workbook.Write(file);
//这句代码非常重要,如果不加,会报:打开的EXCEL格式与扩展名指定的格式不一致
file.Seek(0, SeekOrigin.Begin);

return file;
}

原文地址:https://www.cnblogs.com/workstation-nigoudongma/p/9234035.html