Advertisement

04.10.2008 at 05:13PM PDT, ID: 23313795
[x]
Attachment Details

generating - xml - excel workbook from web page with c#,  i receive an workbook with two sheets but empty, please have a look at the following code.

Asked by toshi_ in Microsoft Visual C#.Net

Tags: c# .NET 2.0

Help!!!

when i generate my file, firefox, i receive the xml as text, and excel doesnt open the file automatiquely.

moreover, if i save the xml file and then open it with excel, my pages are empty.

i can not find the error on the following code,
my xml file is generated, she has the right number of sheets,

what's wrong? helpStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
protected void Page_Load(object sender, EventArgs e)
        {
         
            .....
            ExcelWorkBookController Controller_ = new ExcelWorkBookController(VarSession.Role, VarSession.Community);
            
 
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentType = "text/xml";
            //Response.ContentType = "text/xls";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("iso-8859-2");
            Response.Charset = "";
            this.EnableViewState = false;
            Response.AddHeader("Content-Disposition", "filename=transporterExport_" + DateTime.Now.ToShortDateString() +".xls");
 
            Response.Write(Controller_.WriteExcelWorkBookXML().ToString());
 
            Response.End();
        }
 
 
namespace Ecowaste.E_plan.ApplicationLayer
{
    public class ExcelWorkBookController
    {
 
        private ApplicationConfiguration.SystemConfiguration SC_;
        private DomainLayer.IAccountsRepository AccountRepo_;
        // to export
        private DataSet dsAllData_;
        // sheets
        private DataTable _dt1;
        private DataTable _dt2;
        private StringBuilder sb_;
 
 
        public ExcelWorkBookController(string role, string community)
        {
            SC_ = new SystemConfiguration(role, community);
            PersistenceLayer.PgsAccountsRepository repo_ = new PersistenceLayer.PgsAccountsRepository(role, community);
          
            _dt1 = repo_.FetchAllAccount_ToDT();
                _dt1.TableName = "tata";
            _dt2 = repo_.FetchAllAgencyAccount_ToDT();
                _dt2.TableName = "tete";
            _dt3 = repo_.FetchAllAgencyAccount_ToDT();
                _dt3.TableName = "titi";
 
            dsAllData_ = new DataSet();
            dsAllData_.Tables.Add(_dt1);
            dsAllData_.Tables.Add(_dt2);
 
            sb_ =  new StringBuilder();
        }
 
 
 
#region xml excel export
        public StringBuilder WriteExcelWorkBookXML()
        {
            //System.Configuration.ConfigurationSettings.AppSettings.Get("DOWNLOAD_USERS_REPOSITORY");
 
            sb_.Append("<?xml version=\'1.0'?>\n<?mso-application progid='Excel.Sheet'?>");
            sb_.Append("\n<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet' ");
            sb_.Append("\n xmlns:o='urn:schemas-microsoft-com:office:office' ");
            sb_.Append("\n xmlns:x='urn:schemas-microsoft-com:office:excel' ");
            sb_.Append("\n xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'");
            sb_.Append("\n xmlns:html='http://www.w3.org/TR/REC-html40'>");
            sb_.Append("\n<ExcelWorkbook xmlns='urn:schemas-microsoft-com:office:excel'/>");
 
            sb_.Append("\n<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>");
            sb_.Append("\n <Author>toshiro</Author>");
            sb_.Append("\n <LastAuthor>toshiro</LastAuthor>");
            sb_.Append("\n <Created>2007-09-17T20:28:02Z</Created>");
            sb_.Append("\n <LastSaved>2007-09-17T20:33:44Z</LastSaved>");
            sb_.Append("\n <Company>home</Company>");
            sb_.Append("\n <Version>11.5606</Version>");
            sb_.Append("\n</DocumentProperties>");
 
            sb_.Append("\n<Styles>");
            sb_.Append("\n <Style ss:ID='sColHeader'>");
            sb_.Append("\n <Alignment ss:Vertical='Bottom' ss:WrapText='1'/>");
            sb_.Append("\n <Borders>");
            sb_.Append("\n  <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#000000'/>");
            sb_.Append("\n  <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#000000'/>");
            sb_.Append("\n  <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#000000'/>");
            sb_.Append("\n  <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1' ss:Color='#000000'/>");
            sb_.Append("\n  </Borders><Font ss:Bold='1'/>");
            sb_.Append("\n </Style>");
            sb_.Append("\n<Style ss:ID='Default' ss:Name='Normal'>");
            sb_.Append("<Alignment ss:Vertical='Bottom'/><Borders/><Font/><Interior/><NumberFormat/><Protection/></Style>");
            //sw.Write("<Style ss:ID='sCurrency' ss:Name='Currency'><NumberFormat ss:Format='_(&quot;$&quot;* #,##0.00_);_(&quot;$&quot;* \(#,##0.00\);_(&quot;$&quot;* &quot;-&quot;??_);_(@_)'/></Style>");
            //sw.Write("<Style ss:ID='sNumber' ss:Name='Comma'><NumberFormat ss:Format='_(* #,##0.00_);_(* \(#,##0.00\);_(* &quot;-&quot;??_);_(@_)'/></Style>");
            sb_.Append("\n</Styles>");
 
 
            // ---------------------------------------------------------
            foreach (DataTable table in dsAllData_.Tables)
            {
                WriteExcelWorkSheetXML(table.Copy(), sb_);
            }
            // ---------------------------------------------------------
 
            sb_.Append("\n</Workbook>");
 
            return sb_;
            //Response.TransmitFile(sb_.ToString());
            //Response.WriteFile(sb_.ToString());
            //Response.End();
        }
 
 
 
        private void WriteExcelWorkSheetXML(DataTable dt, StringBuilder w)
        {
            //DataRow row = new DataRow();
            //DataColumn col = new DataColumn();
 
            w.Append("\n<Worksheet ss:Name='" + dt.TableName + "'>");
            w.Append("\n<table ss:expandedcolumncount='" + dt.Columns.Count + "' ss:expandedrowcount='" + (dt.Rows.Count + 1) + "'  x:fullcolumns='1' x:fullrows='1'>");
 
            w.Append("\n<Row ss:AutoFitHeight='0'>");
            foreach (DataColumn col in dt.Columns)
            {
                //w.Write("\n\t\t<Cell ss:StyleID='sColHeader'><Data ss:Type='String'>" +col.ColumnName+ "</Data></Cell>");
                //w.Write("\n\t\t<Cell ss:StyleID='sColHeader'><Data ss:Type='String'>" +col.ColumnName+ "</Data></Cell>");
                w.Append("\n <Cell><Data ss:Type='String'>" + col.ColumnName + "</Data></Cell>");
            }
            w.Append("\n</Row>");
 
            foreach (DataRow row in dt.Rows)
            {
                w.Append("\n<Row>");
                foreach (DataColumn col in dt.Columns)
                {
                    //w.Write("\n\t\t<Cell ss:StyleID='"  +GetExcelColumnStyle(col, "Style")+ "' ><Data ss:Type='" + GetExcelColumnStyle(col, "Type") + "'>"  +row[col]+  "</Data></Cell>");
                    w.Append("\n <Cell><Data ss:Type='" + GetExcelColumnStyle(col, "Type") + "'>" + row[col] + "</Data></Cell>");
                }
                w.Append("\n</Row>");
            }
 
            w.Append("\n</table>");
            w.Append("\n</Worksheet>");
        }
 
 
 
        public string GetExcelColumnStyle(DataColumn col, string outputFormat)
        {
            string retour = "";
            if (outputFormat == "Style")
            {
                switch (col.DataType.ToString())
                {
                    case "System.Decimal":
                        retour = "sCurrency";
                        break;
                    case "System.String":
                        retour = "Default";
                        break;
                    default:
                        retour = "sNumber";
                        break;
                }
            }
            else if (outputFormat == "Type")
            {
                switch (col.DataType.ToString())
                {
                    case "System.Decimal":
                        retour = "Number";
                        break;
                    case "System.Int32":
                        retour = "Number";
                        break;
                    default:
                        retour = "String";
                        break;
                }
            }
 
            return retour;
        }
        #endregion
    }
}
[+][-]04.10.2008 at 05:52PM PDT, ID: 21330667

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.11.2008 at 02:00AM PDT, ID: 21332345

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.11.2008 at 07:29AM PDT, ID: 21334575

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.12.2008 at 09:57AM PDT, ID: 21341785

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.13.2008 at 05:46AM PDT, ID: 21344545

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.13.2008 at 11:37PM PDT, ID: 21347940

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.14.2008 at 06:26AM PDT, ID: 21349752

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual C#.Net
Tags: c# .NET 2.0
Sign Up Now!
Solution Provided By: TheLearnedOne
Participating Experts: 1
Solution Grade: B
 
 
[+][-]04.14.2008 at 07:06AM PDT, ID: 21350100

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.14.2008 at 08:38AM PDT, ID: 21350946

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628