Nevron Gauge for SharePoint
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » Silverlight 5 » Excel file cannot open when export data from datagrid to excel ?
       
Author Reply
kannan s
posted 46 posts
since Feb 17, 2010 
from

Excel file cannot open when export data from datagrid to excel ?

  Posted on: 21 Aug 2010       

Hi,
Excel file cannot open when export data  from  datagrid to excel. when i open excel it displays the below error,
"Excel found unreadable content in 'data.xls'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes."
Ankur Gupta
posted  59 posts
since  May 29, 2008 
from 

 Re: Excel file cannot open when export data from datagrid to excel ?
  Posted on: 21 Aug 2010        0  

Hello
It seems that the process to save the xls file is going wrong.
Can you put your code here so that i can help ?

Ankur Gupta

BCA, MCA, MBA, Mindcracker MVP 

kannan s
posted  46 posts
since  Feb 17, 2010 
from 

 Re: Excel file cannot open when export data from datagrid to excel ?
  Posted on: 21 Aug 2010        0  

public static void ExportToExcel (DataGrid datagrid, Stream stream)
{
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("Sheet 1");


datagrid.UpdateLayout();
// IList source = datagrid.ItemsSource as IList ;
List<Test> source = new List<Test>();
for (int i = 0; i < datagrid.Columns.Count -1 ; i++)
{
source.Add(
new Test() { Name = i.ToString() });
}
if (source == null)
return;
//Write column headers
for (int i = 0; i < datagrid.Columns.Count - 1; i++)
worksheet.Cells[0, i] =
new Cell(datagrid.Columns[i].Header);

int rowIndex = 1; //Move up one row
int colIndex = 0;
foreach (Object data in datagrid.ItemsSource)
{
datagrid.ScrollIntoView(data,
null);
for (int i = 0; i < datagrid.Columns.Count; i++)
{
string content = string.Empty;
FrameworkElement element = datagrid.Columns[i].GetCellContent(data);
if (element != null)
{
if (element is TextBlock)
{
content = (element
as TextBlock).Text;
}
else if (element is HyperlinkButton)
{
content = (element
as HyperlinkButton).Content.ToString();
}

}
//Write cell content
worksheet.Cells[rowIndex, colIndex] =
new Cell(content);
colIndex++;
}
rowIndex++;
colIndex = 0;
}
datagrid.ScrollIntoView(source[0],
null);
workbook.Worksheets.Add(worksheet);
workbook.Save(stream);

}

 
       
Nevron Gauge for SharePoint
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved