EPPlus生成Excel表格(只支持2007及以上)
阿新 • • 發佈:2017-12-22
false 生成 and setsize example 打開 utl demo isp
FileInfo newFile = new FileInfo(@"F:\mynewfile.xlsx"); using (ExcelPackage xlPackage = new ExcelPackage(newFile))//如果mynewfile.xlsx存在,就打開它,否則就在該位置上創建 { ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("Tinned Goods"); worksheet.Cells[簡單使用(單元格賦值、使用公式等)1, 1].Value = "Product"; worksheet.Cells[2, 1].Value = "Broad Beans"; worksheet.Cells[3, 1].Value = "String Beans"; worksheet.Cells[4, 1].Value = "Peas"; worksheet.Cells[5, 1].Value = "Total"; worksheet.Cells[1, 2].Value = "Tins Sold";//給單元格賦值 ExcelRange cell = worksheet.Cells[2, 2]; cell.Value = 15;//另一種方式給單元格賦值 string calcStartAddress = cell.Address; worksheet.Cells[3, 2].Value = 32; worksheet.Cells[4, 2].Value = 65;string calcEndAddress = worksheet.Cells[4, 2].Address; worksheet.Cells[5, 2].Formula = string.Format("SUM({0}:{1})", calcStartAddress, calcEndAddress);//使用公式計算值,並賦值給單元格 worksheet.Column(1).Width = 15;//設置列寬 xlPackage.Workbook.Properties.Title = "Sample 1";//設置excel的一些屬性 xlPackage.Workbook.Properties.Author = "John Tunnicliffe"; xlPackage.Workbook.Properties.SetCustomPropertyValue("EmployeeID", "1147"); xlPackage.Save();//保存Excel表格
using (ExcelPackage pck = new ExcelPackage()) { //Create the worksheet ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo"); //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1 ws.Cells["A1"].LoadFromDataTable(tbl, true); //Format the header for column 1-3 using (ExcelRange rng = ws.Cells["A1:C1"]) { rng.Style.Font.Bold = true; rng.Style.Fill.PatternType = ExcelFillStyle.Solid; //Set Pattern for the background to Solid rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189)); //Set color to dark blue rng.Style.Font.Color.SetColor(Color.White); } //Example how to Format Column 1 as numeric using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1]) { col.Style.Numberformat.Format = "#,##0.00"; col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right; } //Write it back to the client Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx"); Response.BinaryWrite(pck.GetAsByteArray()); }在Web中使用
FileInfo newFile = new FileInfo(@"F:\mynewfile.xlsx"); using (ExcelPackage xlPackage = new ExcelPackage(newFile)) { // get the first worksheet in the workbook ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1]; int iCol = 2; for (int iRow = 1; iRow < 6; iRow++) { string valueStr = string.Format("Cell({0},{1}).Value={2}", iRow, iCol, worksheet.Cells[iRow, iCol].Value);//循環取出單元格值 string value_Str = string.Format("Cell({0},{1}).Formula={2}", 6, iCol, worksheet.Cells[6, iCol].Formula);//取公式(失敗了) } }讀取Excel表格中的內容
FileInfo newFile = new FileInfo(@"F:\mynewfile.xlsx"); ExcelPackage pck = new ExcelPackage(newFile); //Add the Content sheet var ws = pck.Workbook.Worksheets.Add("Content"); #region 縮略column ws.View.ShowGridLines = false; ws.Column(4).OutlineLevel = 1;//0表示沒有線 ws.Column(4).Collapsed = true;//合並 ws.Column(5).OutlineLevel = 1; ws.Column(5).Collapsed = true; ws.OutLineSummaryRight = true; ws.Cells["B1"].Value = "Name"; ws.Cells["C1"].Value = "Size"; ws.Cells["D1"].Value = "Created"; ws.Cells["E1"].Value = "Last modified"; ws.Cells["B1:E1"].Style.Font.Bold = true; #endregion #region 添加圖片到Excel中 Bitmap icon = new Bitmap(@"F:\3765249-468df6edf927b569.jpg"); int row = 5; ws.Row(row).Height = 125;//設置整個第五行的高度 //Add the icon as a picture if (icon != null) { ExcelPicture pic = ws.Drawings.AddPicture("pic" + (row).ToString(), icon); pic.SetPosition((int)20 * (row - 1) + 2, 0);//margin-left:0px; margin-top:(int)20 * (row - 1) [20:默認的單元格高度] } ws.Cells[3, 3].Formula = string.Format("SUBTOTAL(9, {0})", ExcelCellBase.GetAddress(3 + 1, 3, row - 1, 3)); #endregion #region 定義一塊矩形,自由填寫文字 var shape = ws.Drawings.AddShape("txtDesc", eShapeStyle.Rect); shape.SetPosition(7, 10, 7, 10);//(第7行,向下偏移10px,第7列,向右偏移10px) shape.SetSize(400, 200); shape.Text = "這是一塊自定義的區域, Shapes and charts.\n\r\n\r這是換行之後的內容..."; shape.Fill.Style = eFillStyle.SolidFill; shape.Fill.Color = Color.DarkSlateGray; shape.Fill.Transparancy = 20;//透明度 shape.Border.Fill.Style = eFillStyle.SolidFill; shape.Border.LineStyle = eLineStyle.LongDash; shape.Border.Width = 1; shape.Border.Fill.Color = Color.Black; shape.Border.LineCap = eLineCap.Round; shape.TextAnchoring = eTextAnchoringType.Top; shape.TextVertical = eTextVerticalType.Horizontal; shape.TextAnchoringControl = false; #endregion #region 超鏈接 var namedStyle = pck.Workbook.Styles.CreateNamedStyle("HyperLink"); //This one is language dependent namedStyle.Style.Font.UnderLine = true; namedStyle.Style.Font.Color.SetColor(Color.Blue); ws.Cells["K12"].Hyperlink = new ExcelHyperLink(@"A51", "Statistics");//在K12單元格設置一個超鏈接,點擊該超鏈接可以快速定位到A51單元格 ws.Cells["K12"].StyleName = "HyperLink"; #endregion pck.Save();//保存Excel表格縮略column,添加圖片到Excel中,定義一塊矩形填寫內容,超鏈接
EPPlus生成Excel表格(只支持2007及以上)