Cells cells = sheet.Cells; Style style = workbook.Styles[workbook.Styles.Add()]; style.Font.IsBold = true; style.Font.Name = "宋體"; style.Font.Size = 12; //固定模板頭 //居中、畫邊框、粗體、背景色為淺藍 style = workbook.Styles[workbook.Styles.Add()]; style.HorizontalAlignment = TextAlignmentType.Center; //文字居中 style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style.ForegroundColor = System.Drawing.Color.FromArgb(191, 191, 191); style.Pattern = BackgroundType.Solid; style.Font.IsBold = true; //采樣日期的格式 Style dateStyle = workbook.Styles[workbook.Styles.Add()]; dateStyle.Custom = "yyyy-MM-dd"; //采樣時分的格式 Aspose.Cells.Style timeStyle = workbook.Styles[workbook.Styles.Add()]; timeStyle.Custom = "hh:mm"; //設置行高 cells.SetRowHeight(rowNum, 30); sheet.FreezePanes(ROW_DATASTART, 0, ROW_DATASTART, 0); //凍結列頭內容 //列頭設置統一樣式 style.Font.Color = System.Drawing.Color.FromArgb(0, 0, 0); Range dateDeatailRange = sheet.Cells.CreateRange(0, 0, ROW_DATASTART, colNum); StyleFlag dateDeatailFlg = new StyleFlag(); dateDeatailFlg.All = true; dateDeatailRange.ApplyStyle(style, dateDeatailFlg);/*設置單元格樣式-方法2 */ dateDeatailRange.RowHeight = 20.0;//設置行高 var contentStyle = cells[i + 1, j].GetStyle();//得到原本樣式 contentStyle.HorizontalAlignment = TextAlignmentType.Center;//在原本樣式的基礎上設置新的樣式 cells[i + 1, j].SetStyle(contentStyle); //重新設置 sheet.AutoFitColumns(); //自適應列寬 sheet.AutoFitRows(); //自適應行高 //隱藏不需要的行 cells.HideRow(ROW_LHCODEID); //單元格設置值 sheet.Cells[rowNum, j].PutValue("內容");
//移除某列 sheet.Cells.DeleteColumn(9);
//自定義格式,按顯示值來設置顯示小數位 string strCustom = "##0"; if (disDegit > 0) { strCustom += "." + "0".PadLeft(disDegit, '0'); } Style cleStyle = sheet.Cells[rowNum, colNum].GetStyle(); cleStyle.Custom = strCustom; sheet.Cells[rowNum, colNum].Value = AvgValueView; //OR sheet.Cells[rowNum, colNum].PutValue(AvgValueView, true);
Aspose.cell 給excel表格設置樣式
//設置單元格樣式 注意:樣式可以被替換,多次設置(Range)會已最后一次為准 Aspose.Cells.Style cellStyle = cells[rowNum, colNum].GetStyle(); cellStyle.Font.Name = "宋體"; cellStyle.Font.Size = 11; //設置邊框 cellStyle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; cellStyle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; cellStyle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; cellStyle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //設置背景色 cellStyle.Pattern = BackgroundType.Solid; cellStyle.ForegroundColor = System.Drawing.Color.Red; //背景色漸變 cellStyle.Pattern = BackgroundType.Solid; var cl1 = System.Drawing.Color.FromArgb(0, 255, 255, 204);//RGB反序 var cl2 = System.Drawing.Color.FromArgb(0, 0, 0, 255);//RGB反序 cellStyle.SetTwoColorGradient(cl1, cl2, Aspose.Cells.Drawing.GradientStyleType.Vertical, 1);
//批量插入DataTable數據,不插入表頭,從A3行開始插入 int iCount = sheet.ImportDataTable(dtData, false, "A3"); //復制多行表頭(CopyRow) sheet.Cells.CopyRows(sheet.Cells, 0, 0, 2);
//復雜報表 WorkbookDesigner designer = new WorkbookDesigner(); DataTable dtExport = new DataTable(); dtExport.TableName = "A"; dtExport.Columns.Add("AreaName"); //加載模版 designer.Open(strSaveFilePath); //導入Table數據 designer.SetDataSource(dtExport); designer.Process(); //轉換為workbook Workbook workbook = designer.Workbook; workbook.Worksheets[0].Name = "sheet1"; workbook.Worksheets[1].Name = "sheet2"; designer.ClearDataSource();
復雜報表參考:
https://www.cnblogs.com/wuhuacong/archive/2011/02/23/1962147.html
https://blog.csdn.net/kongwei521/article/details/41647747
//保存多種文件格式,包括Xlsx workbook.Save("C:\\Test.xlsx", SaveFormat.Xlsx); //轉換為系統內存提供流式--輸出流 workbook.SaveToStream(); //其他初始化格式 Workbook workbook = new Workbook(FileFormatType.Xlsx); //Some code. workbook.FileFormat = FileFormatType.Xlsx;