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

How can classes be passed by reference in .NET?
Run Web-Service and Client on two computers
Cannot programmaticlly peek into a remote (private) msmq: Access to Message Queuing system is denied
Is there any hidden functionality behind AsNoTracking()?
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll, whenever try to run the web application
NavigationUI Functionality in Custom Button
Private chat in asp.net website [closed]
LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method error occurs with nullable fields
Move gameobject to click position in unity 2d
Sonarqube & Dotcover - Coverage Metric is N/A or '-'
Access Network Drive That is Marked Red Rather than Green Before Running a Process That Accesses a File on The Network Drive
Use factory pattern when using different properties based on a property value
How to link a cshtml page using href in mvc4
how to add multiple comboboxes to the listbox dynamically in C#
WinForms Layered Controls with Background images cause tearing while scrolling
Computational and stylistic optimal solution for the conditional where clause using Linq

Categories

HOME
airflow
oop
algolia
ns2
opencv4android
liquibase
arcgis
boxplot
lithium
sms
terminal
autocad
kendo-asp.net-mvc
rmarkdown
swift3.1
loss
openscad
beacon
angular2-pipe
ithit-webdav-server
android-cursorloader
setup-project
azure-table-storage
csproj
cqlsh
concrete5
textview
blockly
loopback
contextmenustrip
openvms
apache-falcon
http-proxy
headphones
mkdir
client-server
rtsp
form-fields
jibx
file-conversion
rbac
uitextview
firefox-developer-edition
openxava
monaca
appcmd
disassembly
acceleo
scanf
nls
therubyracer
gevent
jira-agile
android-xmlpullparser
db2-zos
sbt-proguard
audiorecord
skbio
instabug
dynamics-nav-2013
properties-file
autoscaling
vectordrawable
minko
pymol
cordova-3
kss
breeze-sharp
vundle
redmine-plugins
rails-migrations
yandex-api
smartxls
webshim
xps
imaplib
code-complete
kendo-window
uikit-dynamics
symbian3
bho
nsautoreleasepool
reference-manual
mmo
boost.build
gpl
doxia
xcode4.6.3
django-admin-tools
win-prolog
advanceddatagrid
commonsware
nsundomanager
copyfile
discussion-board
qvariant
xhtmlrenderer
nscharacterset
jquery-attributes
dongle
qprocess
hp-trim
lts
calendarextender

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App