Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am writing my data into .xls format. Everything is working fine. The only prob is this that I have a particular cell and column whose width I want to reduce as my client doesn't want that. How can I achieve that ? I tried searching but I am unable to get anything helpful ...here is my code to write the file :
C#
 string sFlag = "Error";

        StreamWriter sw = new StreamWriter(filepath, false);
        StringBuilder dataToExport = new StringBuilder();
        sw.WriteLine(Convert.ToString(dataToExport));
        try
        {
            if (filename.Trim() != "" && filepath != "" && dtResult.Rows.Count != 0)
            {
                dataToExport.Append("<table width=\"100%\">");
                dataToExport.Append("<tr>");
                
                dataToExport.Append("<td style=\"height:120px; width:306px;\"> Cybex Exim Solutions (P) Ltd.");
                dataToExport.Append("<br/>xyz,<br />abc ");
                dataToExport.Append("<br/>Tel: 123456789; Fax: 123456789");
                dataToExport.Append("<br/>Email:xyz@abc.com, Web:- www.my.com");
                dataToExport.Append("</td>");
                dataToExport.Append("</tr>");
                dataToExport.Append("<tr>");
               
                foreach (DataColumn dCol in dtResult.Columns)
{                {
                    dataToExport.Append("<td style=\"border:solid 1px black; background-color:orange;\">");
                    dataToExport.Append(HttpContext.Current.Server.HtmlEncode(dCol.ColumnName));
                    dataToExport.Append("</td>");
                    
                }
             

                dataToExport.Append("</tr>");

                foreach (DataRow dRow in dtResult.Rows)
                {
                    dataToExport.Append("<tr>");
                    foreach (object obj in dRow.ItemArray)
                    {
                        dataToExport.Append("<td style=border:solid 1px blue;>");
                        dataToExport.Append(HttpContext.Current.Server.HtmlEncode(obj.ToString()));
                        dataToExport.Append("</td>");
                      
                    }
                   
                    dataToExport.Append("</tr>");
                }

                dataToExport.Append("</table>");

            }
            sw.WriteLine(Convert.ToString(dataToExport));
            sFlag = "Success";

            sw.Close();


In the code above, after completing my company's address. the first column is for date that is taking unnecessary space. Please help me to reduce that.
Posted
Updated 22-Aug-12 20:44pm
v2

This post[^] might help. Or this[^].

In addition, Summarize C# Control Excel Skills[^] could help you.
A similar question was also asked here[^].
 
Share this answer
 
Comments
Taresh Uppal 23-Aug-12 2:42am    
I tried this but I am saving the files on folder on my server. So creating duplicate files will create a issue of memory management. Don;t we have any other solution..? If u can help me bit more :(
Taresh Uppal 25-Aug-12 1:49am    
this is the whole code...
which column's width do U want to reduce...can you highlight the piece of code...
 
Share this answer
 
Comments
Taresh Uppal 23-Aug-12 2:47am    
In the start I write my Company's info with a pic. After that data is written using column and rows. After my company's website is written there succesfully code enters here
foreach (DataColumn dCol in dtResult.Columns) <---- The first Column that Starts and actually having DATA
{
dataToExport.Append("<td style=\"border:solid 1px black; background-color:orange;\">");
dataToExport.Append(HttpContext.Current.Server.HtmlEncode(dCol.ColumnName));
dataToExport.Append("</td>");

}

and writes column for date, which is taking more space and I know that this is due to that info that I am writing. Because the column is taking the same width as that info column on the top is taking. So this is my problem. Company info is large and under that date comes as first column which doesn;t needs to take dat same space. Hope I am able to explain it better now.
senguptaamlan 24-Aug-12 2:29am    
change the width/height in the script and try it...as I can't see the whole code...it is a bit tough to imagine your desired output...

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