使用指定的列宽导出到Excel [英] Export to excel with specified column width

查看:72
本文介绍了使用指定的列宽导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想将一些数据导出到excel中。第一列,第三列和第五列中的数据应取自数据库,第二列和第四列应为null,并且列宽应为2.5单位。我怎么能这样做?



谢谢。

Hi,

I want to export some data into excel. Data in the first,third and fifth column should taken from the database, second and fourth column should be null and should have a column width of 2.5 unit. How can I do it?

Thank you.

推荐答案

除了使用之外别无他法这种版本的互操作:

http://support.microsoft.com/kb/316383 [ ^ ]



其他支持的Interops:

http:// msdn。 microsoft.com/en-us/library/15s06t57.aspx [ ^ ]



现在,关闭你去编程...;)
There is no other way than to use interop for this kind of editiong:
http://support.microsoft.com/kb/316383[^]

Other supported Interops:
http://msdn.microsoft.com/en-us/library/15s06t57.aspx[^]

Now, off you go and program... ;)


我已经使用以下函数进行Excel导出。



I've used the following function for Excel export.

private void ExporttoExcel(DataTable table)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.Write(@");
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");

        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        //sets font
        HttpContext.Current.Response.Write("<font style="font-size:10.0pt; font-family:Calibri;">");
        HttpContext.Current.Response.Write("<br><br><br>");
        //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
        HttpContext.Current.Response.Write("<table border="1" bgcolor="#ffffff" hold=" />          " bordercolor="#000000" cellspacing="0" cellpadding="0" style="font-size:10.0pt; font-family:Calibri; background:white;"> <tr>");
        ////am getting my grid's column headers
        //int columnscount = g .Columns.Count;

        //for (int j = 0; j < columnscount; j++)
        //{      //write in new column
        //    HttpContext.Current.Response.Write("<td>");
        //    //Get column headers  and make it as bold in excel columns
        //    HttpContext.Current.Response.Write("");
        //    HttpContext.Current.Response.Write(GridView_Result.Columns[j].HeaderText.ToString());
        //    HttpContext.Current.Response.Write("");
        //    HttpContext.Current.Response.Write("</td>");
        //}
        HttpContext.Current.Response.Write("</tr>");
        foreach (DataRow row in table.Rows)
        {//write in new row
            HttpContext.Current.Response.Write("<tr>");
            
            HttpContext.Current.Response.Write("<td>");
            HttpContext.Current.Response.Write(row[0].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td style="width: 2.5px;">");
            HttpContext.Current.Response.Write(row[1].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td>");
            HttpContext.Current.Response.Write(row[2].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td style="width: 2.5px;">");
            HttpContext.Current.Response.Write(row[3].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td>");
            HttpContext.Current.Response.Write(row[4].ToString());
            HttpContext.Current.Response.Write("</td>");
            HttpContext.Current.Response.Write("<td>");
            HttpContext.Current.Response.Write(row[5].ToString());
            HttpContext.Current.Response.Write("</td>");

            HttpContext.Current.Response.Write("</tr>");
        }
        HttpContext.Current.Response.Write("</table>");
        HttpContext.Current.Response.Write("</br></br></br></font>");
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }


这篇关于使用指定的列宽导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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