c#
How to set the worksheet name with Excel Interop
using (XLWorkbook wb = new XLWorkbook()) { wb.Worksheets.Add(dt); wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; wb.Style.Font.Bold = true; Response.Clear(); Response.Buffer = true; Response.Charset = ""; Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment;filename= EmployeeReport.xlsx"); using (MemoryStream mymemorystream = new MemoryStream()) { wb.SaveAs(mymemorystream); mymemorystream.WriteTo(Response.OutputStream); Response.Flush(); Response.End(); } } The error message I'm getting is Worksheet names cannot be empty. How do I solve this?
The returned object of a worksheet adding operation is the worksheet itself. Change the worksheet name on that object: var ws = wb.Worksheets.Add(dt); ws.Name = "my sheet name"; Also, because you're using a DataTable as your source for the sheet, you have to make sure the table has a name: dt.TableName = "my sheet name"; MSDN reference: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.name.aspx?f=255&MSPPError=-2147217396
Related Links
C# Passing an array of Func<T, List<myClass>> to a method
C# .NET Memory Management with Data Structures (Dictionary, List, etc.)
C# Debug Attribute
Using a DataTable of a Sharepoint list, how can I preserve the LookupID
c# split line with .txt
cache entry replacement algorithm
How to show the PI tagsearch dialog and return tagname as string?
How do I format my SQL in my XML file such that C# doesn't think the query is part of the XML?
Composite Design pattern & common descendant methods
Compiler having trouble with referencing an assembly
Extending enums in c#
Why is my button's 'Validating' event handler never called?
C# calling native C++ all functions: what types to use?
How can i use EF Code First to declare a one-to-many relationship?
access data in table in .net as in ruby-on-rails
convert cartesian coordinate to picturebox coordinates c#