使用C#将数据导出到Excel时出现问题

编程入门 行业动态 更新时间:2024-10-09 07:26:08
本文介绍了使用C#将数据导出到Excel时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用数据表将数据导出到Excel.它工作正常,并在Excel中提供了许多列.但是,我想添加一些自定义列并使其加粗,这些自定义列应出现在Excel中的数据表列上方. 如果我可以更改其颜色,请告诉我.请参见下面的代码.

Hi, I am exporting data into Excel using a datatable. It is working fine and giving me a number of columns in Excel. However, I want to add and make bold some custom column which should appear above the datatable column in Excel. If I can change their color then please let me know. See code below.

public void ExportToSpreadsheet(DataTable table, string name) { HttpContext context = HttpContext.Current; context.Response.Clear(); string ColValue; string ColName = ""; for (int i = 0; i < table.Columns.Count; i++) { ColName = table.Columns[i].ColumnName.ToString() + "\t"; context.Response.Write(ColName); } context.Response.Write(Environment.NewLine); foreach (DataRow row in table.Rows) { for (int i = 0; i < table.Columns.Count; i++) { //row[i] = row[i].ToString().Trim().Replace("\t", string.Empty) ; ColName = table.Columns[i].ColumnName.ToString(); DateTime tempDate = new DateTime(); if (DateTime.TryParse(row[i].ToString().Trim(), out tempDate) == true) ColValue = tempDate.ToString("dd/MM/yyyy"); else { ColValue = row[i].ToString(); } ColValue = ColValue.ToString().Replace(",", string.Empty) + "\t"; ColValue = ColValue.ToString().Replace(Environment.NewLine, " "); ColValue = ColValue.ToString().Replace("\n", " "); ColValue = ColValue.ToString().Replace("&nbsp;", ""); ColValue = ColValue.ToString().Replace("-Select-;", ""); if (table.Columns[i].DataType.Name == "Boolean") { ColValue = ColValue.ToString().Replace("True", "Yes"); ColValue = ColValue.ToString().Replace("False", "No"); } context.Response.Write(ColValue); } context.Response.Write(Environment.NewLine); } context.Response.ContentType = "application/ms-excel"; context.Response.AppendHeader("Content-Disposition", "attachment; filename = " + name + ".xls"); context.Response.End(); }

推荐答案

在没有自动化Excel的情况下,您实际上不应使用服务器端代码来做到这一点(无论如何也可能会遇到权限问题),这是一种实现这是使用上面类似的代码,但是输出有效的HTML并以.xls扩展名保存. 这将使您可以加粗某些列并应用常规的HTML样式. 例如,在此处查看示例 forums.asp/t/1214938.aspx [ ^ ] 例如,在您的计算机上创建一个新的文本文件.将以下HTML粘贴到其中&保存文件.然后将文件扩展名更改为.xls&在Excel中打开它.请注意,我们有大胆的标题和红色"样式. Without automating Excel, which you shouldn''t really do using server side code (and may run into permissions issues anyway), a way to achieve this is to use the similar code above but output valid HTML and save with the .xls extension. This will let you bold certain columns + apply general HTML styling. e.g look at examples here forums.asp/t/1214938.aspx[^] For example, create a new text file on your computer. Paste the following HTML into it & save the file. Then change the file extension to .xls & open it in Excel. Notice we''ve got bold headers + a ''red'' style. <HTML> <HEAD> <style type='text/css'> table { cellpadding: 0px; font-size: xx-small; border-collapse:collapse; } td.some-style { border: 1px solid black; color: red; } </style> </HEAD> <BODY> <TABLE border=0> <TR> <Td></Td> <Td></Td> <Td></Td> <Td class="some-style">This is an example</Td> <Td></Td> </TR> <thead> <TR> <Th> ProductID</Th> <Th> Product Name</Th> <Th> Sales Person</Th> <Th> Date Closed</Th> <Th> Sale Amount</Th> </TR> </thead> <TR> <TD>1</TD> <TD>Code Project</TD> <TD>Dylan Morley</TD> <TD>10/03/2010</TD> <TD>50.00</TD> </TR> <TR> <TD>2</TD> <TD>Microsoft</TD> <TD>Dylan Morley</TD> <TD>10/03/2010</TD> <TD>50.00</TD> </TR> </TABLE> </BODY> </HTML>

您可以在导出例程中轻松创建此HTML 注意:这仅是对于安装了Excel 2003或更高版本的计算机的有效解决方案,我认为这是何时首次引入HTML功能?

You could create this HTML quite easily in your export routine NB: This would only be a valid solution for machines with Excel 2003 or greater installed, which I think is when then first introduced HTML capabilities?

更多推荐

使用C#将数据导出到Excel时出现问题

本文发布于:2023-10-31 10:32:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:导出到   数据   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!