前后端分離,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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM