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

Breaking change in method overload resolution in C# 6 - explanation?
#Url.RouteUrl returns misleading/wrong path for a controller-action (unless the 'routeName' parameter is explicitly specified)
Entity Framework - Loading specific Navigational Property for members in collection
How to create a border for the text in the TextBlock?
Pass a UIElement class parameter to an assembly class?
Simple way to get MAC address from inputted IP?
Using bindings for dependency property XAML
Resize PictureBox with its related controls
Get value inside XElement
How to limit DocuSign request to either wet signing or signature drawing
Query VMWare by ManagedObjectReference?
AdmobAPI/Admob.cs(12,40): warning CS0067: The event `admob.Admob.nativeBannerEventHandler' is never used
Sqlite Batch Insertion Becoming slower after deployment to client's machine
Scripts not running in WebBrowser control
c# cefsharp browser trying to set proxy
Auto return line with a label in C# [duplicate]

Categories

HOME
classification
cors
fortran
formatting
bigtable
dynamic
query-string
schema
point-of-sale
guidewire
tmux
react-select
webseal
dat.gui
jsfiddle
md2
accordion
glibc
matlab-gui
connection-pooling
v4l2
android-wear-2.0
deployment-descriptor
gulp-jshint
typeclass
uibarbuttonitem
osgi-bundle
sphinx4
hierarchy
deferred
snapkit
pyenv
extern
client-server
pygraphviz
compiled-query
wufoo
mef
bioperl
photoshop-script
nservicebus6
selection-sort
group
indoor-positioning-system
particle-system
key-value-store
hoare-logic
salesforce-service-cloud
music-player
database-project
edgar
nfa
xcode5
fedena
javacompiler
indic
disabled-control
code-smell
html5-filesystem
flask-mongoengine
nativequery
maven-javadoc-plugin
jfilechooser
myrrix
jython-2.5
mosix
cocotron
nx
generator-expression
junit-rule
orchestration
big-endian
php-gettext
systrace
erlide
idtabs
log-shipping
vectorwise
soapexception
qvariant
object-tag
moss2007-security
norton
rollover
browser-state
for-xml
focus-stealing
vista64
httpbrowsercapabilities
cons

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App