如何使用C#更改xls文件中单元格的宽度 [英] how to change width of a cell in xls file using c#

查看:64
本文介绍了如何使用C#更改xls文件中单元格的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在将数据写入.xls格式.一切正常.唯一的问题是,我有一个特定的单元格和列,由于我的客户不希望减小其宽度,因此我想减小其宽度.我该如何实现?我尝试搜索,但无法获得任何帮助...这是我的代码来编写文件:

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 :

 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.

推荐答案

[ ^ ].

另外,总结C#控件Excel技能 [此处 [ ^ ].
This post[^] might help. Or this[^].

In addition, Summarize C# Control Excel Skills[^] could help you.
A similar question was also asked here[^].


您要减小哪一列的宽度...您能突出显示一段代码吗...
which column''s width do U want to reduce...can you highlight the piece of code...


这篇关于如何使用C#更改xls文件中单元格的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆