亲测可用!微信小程序实现下载预览文件,导出及保存文件及excel(2)
程序员文章站
2022-07-13 15:30:15
...
后端代码打印
方法1
public async Task<HttpResponseMessage> ExportManage([FromBody]List<ManageDaoChuModel> parameter)
{
if (!parameter.Any())
return null;
//获得数据
var listData = parameter;
var result = new object();
string sFileName = @"App_Data/excels/dataFile.xlsx";
try
{
string URL = string.Format("{0}://{1}/{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, sFileName);
using (ExcelPackage excelPackage = new ExcelPackage())
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("经营数据");
worksheet.DefaultColWidth =20;
worksheet.Cells[1, 1].Value = "日期";
worksheet.Cells[1, 2].Value = "进场总数(kg)";
worksheet.Cells[1, 3].Value = "进场笔数";
worksheet.Cells[1, 4].Value = "进场总额(元)";
worksheet.Cells[1, 5].Value = "交易总数(kg)";
worksheet.Cells[1, 6].Value = "交易笔数";
worksheet.Cells[1, 7].Value = "交易总额(元)";
var rowNum = 2;
foreach (var queryResult in listData)
{
worksheet.Cells["A" + rowNum].Value = (queryResult.updateDate == null||queryResult.updateDate=="") ? "合计" : queryResult.updateDate;
worksheet.Cells["B" + rowNum].Value = queryResult.weightsIn;
worksheet.Cells["C" + rowNum].Value = queryResult.countsIn;
worksheet.Cells["D" + rowNum].Value = queryResult.totalsIn;
worksheet.Cells["E" + rowNum].Value = queryResult.weightsOut;
worksheet.Cells["F" + rowNum].Value = queryResult.countsOut;
worksheet.Cells["G" + rowNum].Value = queryResult.totalsOut;
rowNum++;
}
using (var cells = worksheet.Cells[1, 1, 1, 7])
{
cells.Style.Font.Bold = true;
cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
cells.Style.Fill.BackgroundColor.SetColor(Color.LightGray);
}
//返回二进制字节数组
result = excelPackage.GetAsByteArray();
byte[] byteres = (byte[])result;
var tm = DateTime.Now.ToShortDateString();
string filePath = HttpContext.Current.Server.MapPath("~/") + "App_Data\\excels\\" + "dataFile.xls";
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(byteres, 0, byteres.Length);
fs.Close();
fs.Dispose();
//excelPackage.Save();
FileStream stream = new FileStream(filePath, FileMode.Open);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
//fs.Close();
//fs.Dispose();
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel"); //"application/octet-stream"
response.Headers.Add("Access-Control-Expose-Headers", "FileName");
response.Headers.Add("FileName", HttpUtility.UrlEncode("dataFile.xls"));
return response;
}
}
catch (Exception e)
{
LogHelper.Write(e.Message);
throw this.ErrorProcess(e);
}
//return new FileContentResult((byte[])result, "application/x-xls");
}
打印方法2 (URL链接下载)
[HttpPost]
public string ExportManageUrl([FromBody]List<ManageDaoChuModel> parameter)
{
if (!parameter.Any())
return null;
//获得数据
var listData = parameter;
var result = new object();
string sFileName = @"App_Data/excels/dataFile.xlsx";
string fileName = "";
string URLS = string.Format("{0}://{1}/{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, sFileName);
try
{
using (ExcelPackage excelPackage = new ExcelPackage())
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("经营数据");
worksheet.DefaultColWidth = 20;
worksheet.Cells[1, 1].Value = "日期";
worksheet.Cells[1, 2].Value = "进场总数(kg)";
worksheet.Cells[1, 3].Value = "进场笔数";
worksheet.Cells[1, 4].Value = "进场总额(元)";
worksheet.Cells[1, 5].Value = "交易总数(kg)";
worksheet.Cells[1, 6].Value = "交易笔数";
worksheet.Cells[1, 7].Value = "交易总额(元)";
var rowNum = 2;
foreach (var queryResult in listData)
{
worksheet.Cells["A" + rowNum].Value = (queryResult.updateDate == null || queryResult.updateDate == "") ? "合计" : queryResult.updateDate;
worksheet.Cells["B" + rowNum].Value = queryResult.weightsIn;
worksheet.Cells["C" + rowNum].Value = queryResult.countsIn;
worksheet.Cells["D" + rowNum].Value = queryResult.totalsIn;
worksheet.Cells["E" + rowNum].Value = queryResult.weightsOut;
worksheet.Cells["F" + rowNum].Value = queryResult.countsOut;
worksheet.Cells["G" + rowNum].Value = queryResult.totalsOut;
rowNum++;
}
using (var cells = worksheet.Cells[1, 1, 1, 7])
{
cells.Style.Font.Bold = true;
cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
cells.Style.Fill.BackgroundColor.SetColor(Color.LightGray);
}
//返回二进制字节数组
result = excelPackage.GetAsByteArray();
byte[] byteres = (byte[])result;
var tm = DateTime.Now.ToString("yyyyMMddhhmmssfff");
var tmpath = DateTime.Now.ToString("yyyyMMdd");
var file = "Files\\" + tmpath +"\\"+ tm + ".xls";
var fileUrl = "/Files/" + tmpath +"/"+ tm + ".xls";
string filePath = HttpContext.Current.Server.MapPath("~/") + file;
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(byteres, 0, byteres.Length);
fs.Close();
fs.Dispose();
fileName = fileUrl;
}
return fileName;
}
catch (Exception e)
{
LogHelper.Write(e.Message);
throw this.ErrorProcess(e);
}
//return new FileContentResult((byte[])result, "application/x-xls");
}
下一篇: POI的入门:创建单元格设置数据