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#

Categories

HOME
sql-server-2014
microcontroller
python-requests
c++builder
opencv4android
django-middleware
x509certificate
where
salesforce-lightning
app-store
hibernate-mapping
country-codes
sahi
wowza
webseal
taxonomy
amazon-data-pipeline
css-float
geography
hl7-fhir
jq
descriptor
angular2-pipe
onclicklistener
quality-center
waterfall
ckeditor4.x
akavache
marionette
ds-5
spring-data-gemfire
android-scrollview
matlab-compiler
dotnetnuke-module
susy
google-cloud-powershell
test-kitchen
qt-quick
rexx
infovis
hdpi
emacs24
opticalflow
petapoco
instaparse
system-integration
usps
treelist
web-optimization
jsondoc
gnu-sort
android-facebook
updating
om
tess4j
eaaccessory
pagecontrol
clearquest
transparentproxy
grails-3.0.9
connection-timeout
xmla
grunt-contrib-connect
apache-pivot
fscanf
nest-initiative
disabled-control
switchpreference
xpand
winrun4j
email-headers
dayofweek
git-subtree
netbeans-plugins
mahara
nsnumber
spawn
architectural-patterns
quit
freeimage
openafs
doxia
databus
mysql-connector
fileoutputstream
xpolog
for-xml
high-load
urlscan

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile