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()); }
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);//取公式(失败了) } }
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表格