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

Lambda event handler scope
Get OS-Version in WinRT Metro App C#
Enumerating DataPoints in view
Switching control backColor fast
How can a view fill a model?
Iterating form controls that are created at runtime
How to use a Resource Image in Word document?
detecting type of generics within generics
Stuck with httpwebrequest usage
ScriptManager Error in my code?
How to reduce the number of TCP ACK's during a highly reliable bulk transfer
Is this good practice for a Custom Exception?
Dynamically generate property getter/setter through reflection or similar
Ascending List Permutation
Getting Control level (view) validation errors back to the View Model
Displaying multiple pieces of information from a combobox selection?

Categories

HOME
model-view-controller
ldap
azure-eventhub
apache-storm
jquery-plugins
django-middleware
cryengine
okhttp3
azure-resource-manager
opc-ua
sugarcrm
jshell
atlassian-fisheye
symfony-3.2
virtualhost
ejb-3.0
ethereum
elastalert
suitecrm
osclass
point-of-sale
fastreport
console-application
dompdf
greensock
brightway
simd
jquery-easyui
aiml
react-jsx
dev-c++
typedef
fibonacci
pumping-lemma
chartist.js
core-animation
symlink
mex
launchd
uibarbuttonitem
units-of-measurement
ipython-parallel
recreate
paper-trail-gem
apm
intermec
openvms
machine-code
http-proxy
ansible-inventory
hippocms
iterm2
jvisualvm
collation
firefox-developer-edition
intervention
f#-fake
taleo
niagara-ax
ocamlbuild
opticalflow
stringbuilder
android-music-player
turbolinks
dimensionality-reduction
jspx
rowname
git-cherry-pick
database-administration
ooad
markerspiderfier
google-finance-api
traminer
first-order-logic
vanilla-forums
syncano
google-experiments
sync
xmlserializer
lwuit
azure-worker-roles
rails-migrations
internet-radio
typography
servicemanager
system.transactions
moonscript
socketasynceventargs
mvcmailer
stl-algorithm
log-shipping
openwrap
hwnd
xhtmlrenderer
cinder
coderush-xpress
retrospectiva
cross-cutting-concerns
tracd
paperless

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