Aspose Cells dll 实现数据简单下载

时间:2024-07-18 17:33:44

Workbook workbook = new Workbook(); //工作簿
                    Worksheet sheet = workbook.Worksheets[0]; //工作表
                    sheet.AutoFitRows();
                    sheet.Name = "下载列表";
                    Cells cells = sheet.Cells;//单元格
                    Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
                    //标题
                    style.HorizontalAlignment = TextAlignmentType.Left;//文字左对齐 
                    style.Font.Name = "微软雅黑";//文字字体
                    style.Font.Size = 9;//文字大小 
                    style.HorizontalAlignment = TextAlignmentType.Center;
                    style.ForegroundColor = System.Drawing.Color.Red;

// 文件头

int cellLength = IncomeHeadline.Length;

for (int i = 0; i < IncomeHeadline.Length; i++)

{

cells.SetColumnWidth(i, 15);  cells[0, i].PutValue(IncomeHeadline[i]);   //填写内容

var strCell = cells[0, i].GetStyle();  strCell.ForegroundColor =Color.DodgerBlue;

strCell.Font.Color = Color.White;  strCell.Font.Size = 12;strCell.Pattern = BackgroundType.Solid;

cells[0, i].SetStyle(strCell);

}

//下载设置

HttpContext curContext = HttpContext.Current;
                    // 设置编码和附件格式  
                    curContext.Response.ContentType = "application/vnd.ms-excel";
                    curContext.Response.ContentEncoding = Encoding.UTF8;
                    var fileName = "123“+ ".xls";
                    curContext.Response.AppendHeader("Content-Disposition",
                        "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
                    System.IO.MemoryStream ms = workbook.SaveToStream();//生成数据流
                    byte[] bt = ms.ToArray();
                    curContext.Response.BinaryWrite(bt);
                    curContext.Response.End();