1、使用Aspose.Cell生成Excel文件,Aspose.Cell是.NET組件控件,不依賴COM組件
1首先一點需要使用新建好的空Excel文件做模板,否則容易產生一個多出的警告Sheet
1 Workbook workBookTemp = ExcelHelper.LoadTemplateFile(HttpContext.Current.Server.MapPath("~/../xxx.xlsx")); 2 Workbook workBook = new Workbook(); 3 workBook.Copy(workBookTemp);
ExcelHelper.LoadTemplateFile實現的就是 Workbook workBookTemp = new Workbook(HttpContext.Current.Server.MapPath("~/..."))
2、給Excel每一行添加樣式,兩種標題樣式,小標題樣式類似。
Aspose.Cells.Style styleTitle = StyleTitle(workBook,TextAlignmentType.Center); Aspose.Cells.Style styleSmallTitle = StyleSmallTitle(workBook,TextAlignmentType.Center); //這里把兩種樣式封裝了方法 private Aspose.Cells.Style StyleTitle(Workbook book, TextAlignmentType tape) { Aspose.Cells.Style styleTitle = book.Styles[book.Styles.Add()];//新增樣式 styleTitle.HorizontalAlignment = tape;//文字居中 styleTitle.VerticalAlignment = tape; styleTitle.ForegroundColor = System.Drawing.Color.Silver; styleTitle.IsTextWarpped = true; styleTitle.Pattern = BackgroundType.Solid; styleTitle.IsLocked = true; styleTitle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;//左邊界線 styleTitle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; styleTitle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; styleTitle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; styleTitle.Font.Name = "微軟雅黑"; styleTitle.Font.Size = 10; styleTitle.Font.IsBold = true; return styleTitle; }
3 Aspose.Cells會自動有一個Sheet表單,因此可以直接使用,list是需要導入到表的數據。
workBook.Worksheets[0].Name = "預警報表"; Worksheet sheet = workBook.Worksheets[0]; sheet.AutoFitRows(); EarlyExcel(sheet,list,styleTitle,styleSmallTitle);
4如果需要多個Sheet表單,就需要循環創建sheet;
int index = 0; foreach( var item in DataList) { index++; Worksheet sheet = workBook.Worksheets[index]; .... }
5創建表頭,並添加單元格樣式
private void EarlyExcel(Worksheet sheet, List<Model> list, Aspose.Cells.Style styleTitle, Aspose.Cells.Style styleSmallTitle ) { Cells cells = sheet.Cells; cells[0,0].PutValue("name1"); cells[0,1].PutValue("name2"); ..... //設置列寬,可以提出來一個方法 int columnCount = cells.MaxColumn; int rowCount = cells.MaxRow; for(int col = 0; col < columnCount; col++) { sheet.AutoFitColumn(col,0,rowCount); } for(int col = 0; col < columnCount; col++) { if(col == 2) cells.SetColumnWidth(2,50); else if(col>=0 && col <= 5 && col !=2) cells.SetColumnWidthPixel(col,cells.GetColumnWidthPixel(col) + 60); else cells.SetColumnWidthPixel(col,cells.GetColumnWidthPixel(col) + 30); } //插入值 for(int i = 0; i < list.Count; i ++) { cells[i+1,0].PutValue(list[i].Name1); cells[i+1,2].PutValue(list[i].Name2); ......
//寫入公式的方法
cells[i+1,3].Formula = "SUM(A1:B1)";
cells[i+1,4].Formula = "=AVERAGE(B1:E1)"; } //使用寫好的樣式 for(int j = 0;j < sheet.Cells.MaxDataColumn + 1;j++) { cells[i+1,j].SetStyle(styleSmallTitle); cells.SetRowHeight(i + 1,17); } }
6到這里Excel表單創建完畢,因為我導出的Excel中需要插入圖片。下面列出插入圖片的方法。
//創建的圖片 int pictureIndex = sheet.Pictures.Add(rownum,columnnum,Server.MapPath("~/Images/xxx.png")); setPictureSize(sheet,pictureIndex); private void setPictureSize(Worksheet sheet,int index) { //使用圖片 Aspose.Cells.Drawing.Picture picture = sheet.Pictures[index]; //調圖片位置和寬高 picture.UpperDeltaX = 400; picture.UpperDeltaY = 0; picture.Hieght = 17; picture.Width = 17; }
7寫入到流
System.IO.MemoryStream fileStream = SaveToStream(workBook); public static System.IO.MemoryStream SaveToStream(Workbook workbook) {
//刷新全部公式單元格--當生成的單元格,在修改單元格數據時,需要動態計算取得值的時候使用 workbook.CalculateFormula(true); //輸出到數據流 System.IO.MemoryStream stream = new System.IO.MemoryStream(); workbook.Save(stream,SaveFormat.Excel97To2003); return stream; }
8輸出Excel
SaveToExcel("預警報表",fileStream,DateTime.Now.ToString(),HttpContext.Current); public void SaveToExcel(string fileName , System.IO.MemoryStream fileStream,string time, HttpContext context) { context.Response.Clear(); context.Response.AppendHeader("Content-Disposition",("attachment;filename="+HttpUtility.UrlEncode(fileName.ToString() + "-" + DateTime.Parse(time).ToString("yyyyMMdd") + ".xls")+"")); context.Response.ContentType = "application/octet-stream"; context.Response.ContentEncoding = System.Text.Encoding.UTF7; context.Response.Charset = "gb2312"; context.Response.BinaryWrite(fileStream.ToArray()); context.Response.End(); }
NPOI在下篇寫