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

Add custom event handlers to dynamically created controls[C#]
.Net Core #addTagHelper resolve project assembly
Change row color based on text in column
Selenium automation test runs very slowly compared to manual testing
How to prevent Entity Framework foreign key generation?
How do I go about updating all relative records with my linq instead of first or default?
Why do C# enum types not need to be declared static when used in a static method?
How to contact whole text from file into the string avoiding empty lines beetwen strings
Amazon SMS for single number from c# sdk without creating topic
How to read xml-file using XDocument?
Windows netsh “+” wildcard not working for ServiceHost
why Microsoft Access database is updating in bin debug folder not in local folder?
Prevent ajax post when back button is pressed
Unable to Deserialize json into an ObservableCollection and bind to xaml in Xamarin.Forms
Entity Framework 6.1.3 Hierarchy id not working
Entity Framework select from user specified table

Categories

HOME
actionscript-3
drupal
stata
ide
nagios
livecharts
query-string
kohana
markdown
fastreport
opengrok
hql
cucumberjs
android-actionbar
airbnb
novacode-docx
ithit-webdav-server
akavache
request-uri
pushpad
jbossfuse
r-factor
folder
drawrect
andengine
intermec
spring-oauth2
imdb
postgresql-9.6
bootcamp
mootools
flex3
redhat-brms
smooth-streaming
android-doze-and-standby
mef
automapper-4
perldoc
intermediate-language
catalyst
recursive-query
optional-parameters
visual-web-developer
virtualenvwrapper
class-design
konakart
particle-system
nsoperationqueue
camellia
periodic-processing
ideavim
methodology
ios8-share-extension
magnet-uri
commercetools
updating
android-togglebutton
jsoncpp
xml-simple
eula
teamwork
caption
register-allocation
step
azure-caching
connections
html5-filesystem
data-layers
carrier
lastinsertid
mobile-robots
infomaker
caroufredsel
maven-webstart-plugin
exi
qmainwindow
codeigniter-form-helper
magicsuggest
phone-state-listener
gpl
tasklist
jquery-1.8
commonsware
zend-validate
designated-initializer
nbehave
table-valued-parameters
silverlight-2.0
linq-to-mysql
expander
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