Click here to Skip to main content
15,998,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends...

In my application there is some report generation part ,Currently I am using Excel.Interop Library to generate Workbooks and Worksheets, now its working fine but somewhere I seen that Excel.Interop makes problem in Excel generation some times it work some time it wont... and open xml method is the best one for Excel operations..

So I am checking for some sample stuffs or tutorials regarding this open XML operations, I got some stuff from Code project itself but its for Static gridview not For Dynamic gridviews its not working,

If any one know more about open xml please do replay...
Posted
Comments
Sunny_Kumar_ 16-Jun-12 5:33am    
Hi Tony, can you please explain what do you mean by "dynamically created Gridviews" and share what have you tried ?
Tony Tom.k 18-Jun-12 1:03am    
Dynamically created means there is no grid on the form
on page load we will create Gridview object and bind that data to the gridview...

So actually I dont want to display any gridview I need to get that grid values into excel...

So there are so many set of values in my excel I will create set of gridviews for particular excel fields and on button click redirect to this page it will generate Reports..
All this functionality working fine for Excel.Interop
I need to change this into OpenXml

Hi Tony,

I've drawn a sample of an open xml for excel file. Hope you get a better picture of tags you need to supply to generate an excel compatible file.
You need to define the column, specify styles (if you want them to have), no. of columns and rows and that's it. Iterate through the gridview's cells and put every value for "<ss:cell xmlns:ss="#unknown">" with datatype (ss:Type="String").


XML
<?mso-application progid="Excel.Sheet"??>
<workbook>
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

<styles>
 <style ss:id="Default" ss:name="Normal">
  <alignment ss:vertical="Bottom" />
  <borders />
  <font />
  <interior />
  <numberformat />
  <protection />
 </style>
 <style ss:id="stl1">
  <font x:family="Swiss" ss:color="#000000" ss:bold="1" xmlns:x="#unknown" />
 </style>
 <style ss:id="stl2">
  <font x:family="Swiss" ss:color="#000000" ss:bold="0" xmlns:x="#unknown" />
 </style>
</styles>

<worksheet ss:name="Sheet1">
 <table ss:expandedcolumncount="5" ss:expandedrowcount="5" x:fullcolumns="1" x:fullrows="1" xmlns:x="#unknown">
<column ss:width="50" />
<column ss:width="50" />
<column ss:width="50" />
<column ss:width="50" />
<column ss:width="100" />
<ss:row>
<ss:cell ss:styleid="stl1"><data ss:type="String">Test Col1</data></ss:cell>
<ss:cell ss:styleid="stl1"><data ss:type="String">Test Col2</data></ss:cell>
<ss:cell ss:styleid="stl1"><data ss:type="String">Test Col3</data></ss:cell>
<ss:cell ss:styleid="stl1"><data ss:type="String">Test Col4</data></ss:cell>
<ss:cell ss:styleid="stl1"><data ss:type="String">Test Col5</data></ss:cell>
</ss:row>

<ss:row>
<ss:cell ss:styleid="stl2"><data ss:type="String">Cell Data1</data></ss:cell>
<ss:cell ss:styleid="stl2"><data ss:type="String">Cell Data2</data></ss:cell>
<ss:cell ss:styleid="stl2"><data ss:type="String">Cell Data3</data></ss:cell>
<ss:cell ss:styleid="stl2"><data ss:type="String">Cell Data4</data></ss:cell>
<ss:cell ss:styleid="stl2"><data ss:type="String">Cell Data5</data></ss:cell>
</ss:row>


</table></worksheet>
</workbook>


Hope this helps.

Happy Coding :)
 
Share this answer
 
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GridToExcel.Data;
using GridToExcel.Helper;
using System.IO;

namespace AMC_Main.Reports
{
    public partial class GridXml : System.Web.UI.Page
    {
        DataGrid grdvTest = new DataGrid();
        protected void Page_Load(object sender, EventArgs e)
        {
 
          
        }
        //private void gridBind()
        //{
            
        //}

        protected void btnExport_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            AMC.Business.StudioMain objStudio = new StudioMain();
            DataTable dtsearch = new DataTable();
            dtsearch = objStudio.SearchStudioTemp();
            if (dtsearch.Rows.Count > 0)
            {
                // pnlStudioInfo.Visible = true;
                grdvTest.DataSource = dtsearch;
                ViewState["file"] = dtsearch;
                grdvTest.DataBind();
            }


            

            string file = new ExcelHelper().ExportToExcel(dtsearch);
          //  string file1 = new ExcelHelper().ExportToExcel(dtsearch);
            string rootPath = HttpContext.Current.Server.MapPath("~").ToString();
            string localCopy = Guid.NewGuid().ToString() + ".xlsx";
          //  string localCopy1 = Guid.NewGuid().ToString() + ".xlsx";
            File.Copy(file, rootPath + localCopy);
           // File.Copy(file1, rootPath + localCopy1);
            Response.Redirect(localCopy);

        }
       
    }
}



In ExcelHelper.cs class



C#
internal string ExportToExcel(DataTable table)
        {
            string excelfile = Path.GetTempPath() + Guid.NewGuid().ToString() + ".xlsx";
            using (SpreadsheetDocument excelDoc = SpreadsheetDocument.Create(excelfile, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
            {
                CreateExcelParts(excelDoc, table);
            }
            return excelfile;
        }

        private void CreateExcelParts(SpreadsheetDocument spreadsheetDoc, DataTable data)
        {
            WorkbookPart workbookPart = spreadsheetDoc.AddWorkbookPart();
            CreateWorkbookPart(workbookPart);

            int workBookPartCount = 1;

            WorkbookStylesPart workbookStylesPart = workbookPart.AddNewPart<WorkbookStylesPart>("rId" + (workBookPartCount++).ToString());
            CreateWorkbookStylesPart(workbookStylesPart);

            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>("rId" + (101).ToString());
            CreateWorksheetPart(workbookPart.WorksheetParts.ElementAt(0), data);

            SharedStringTablePart sharedStringTablePart = workbookPart.AddNewPart<SharedStringTablePart>("rId" + (workBookPartCount++).ToString());
            CreateSharedStringTablePart(sharedStringTablePart, data);

            workbookPart.Workbook.Save();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900