[英]exporting to excel from asp.net getting line breaks to display
I have seen a number of questions on this subject - but I can't get the suggestions to work. 我已经看到了与此主题相关的许多问题-但我无法获得建议。 I am exporting a dataset to excel and am using the following to set up the basic formatting.
我正在将数据集导出到excel,并使用以下内容来设置基本格式。 I am setting up the basic formatting by building this string:
我通过构建此字符串来设置基本格式:
StringBuilder sb = new StringBuilder();
sb.AppendFormat(@"<?xml version=""1.0""?>{0}", Environment.NewLine);
sb.AppendFormat(@"<?mso-application progid=""Excel.Sheet""?>{0}", Environment.NewLine);
sb.AppendFormat(@"<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""{0}", Environment.NewLine);
sb.AppendFormat(@" xmlns:o=""urn:schemas-microsoft-com:office:office""{0}", Environment.NewLine);
sb.AppendFormat(@" xmlns:x=""urn:schemas-microsoft-com:office:excel""{0}", Environment.NewLine);
sb.AppendFormat(@" xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""{0}", Environment.NewLine);
sb.AppendFormat(@" xmlns:html=""http://www.w3.org/TR/REC-html40"">{0}", Environment.NewLine);
sb.AppendFormat(@" <Styles>{0}", Environment.NewLine);
sb.AppendFormat(@" <Style ss:ID=""Default"" ss:Name=""Normal"">{0}", Environment.NewLine);
sb.AppendFormat(@" <Alignment ss:Vertical=""Bottom""/>{0}", Environment.NewLine);
sb.AppendFormat(@" <Borders/>{0}", Environment.NewLine);
sb.AppendFormat(@" <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""/>{0}", Environment.NewLine);
sb.AppendFormat(@" <Interior/>{0}", Environment.NewLine);
sb.AppendFormat(@" <NumberFormat/>{0}", Environment.NewLine);
sb.AppendFormat(@" <Protection/>{0}", Environment.NewLine);
sb.AppendFormat(@" </Style>{0}", Environment.NewLine);
sb.AppendFormat(@" <Style ss:ID=""s62"">{0}", Environment.NewLine);
sb.AppendFormat(@" <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""{0}", Environment.NewLine);
sb.AppendFormat(@" ss:Bold=""1""/>{0}", Environment.NewLine);
sb.AppendFormat(@" </Style>{0}", Environment.NewLine);
sb.AppendFormat(@" <Style ss:ID=""s63"">{0}", Environment.NewLine);
sb.AppendFormat(@" <NumberFormat ss:Format=""Short Date""/>{0}", Environment.NewLine);
sb.AppendFormat(@" </Style>{0}", Environment.NewLine);
sb.AppendFormat(@" </Styles>{0}", Environment.NewLine);
sb.Append(@"{0}\r\n</Workbook>");
return sb.ToString();
In a separate function I am replacing 在另一个功能中,我要替换
<br > with
and this works, up to a point. 在某种程度上,这是可行的。 If I have:
如果我有:
Bill<br />Fred<br />Jim
as the data for a cell, it is appearing in the excel spreadsheet as: 作为单元格的数据,它在excel电子表格中显示为:
BillFredJim
If I click the Wrap Text icon in Excel - the text appears as it should within the cell. 如果在Excel中单击“自动换行”图标-文本将在单元格中按原样显示。 Ie as
即作为
Bill
Fred
Jim
I have read that I need to add: 我读到我需要添加:
ss:WrapText=""1""
How can I include that in my StringBuilder so that Excel does it automatically? 如何在我的StringBuilder中包括它,以便Excel自动执行呢?
I tried adding it to the line: 我尝试将其添加到行中:
sb.AppendFormat(@" <Style ss:ID=""Default"" ss:Name=""Normal"">{0}", Environment.NewLine);
so it read: 因此它显示为:
sb.AppendFormat(@" <Style ss:ID=""Default"" ss:WrapText=""1"" ss:Name=""Normal"">{0}", Environment.NewLine);
but, when I try to open the generated excel spreadsheet, it says it is corrupt. 但是,当我尝试打开生成的Excel电子表格时,它说它已损坏。 How can I make it so that when excel opens, the text in the cells wrap?
我如何才能使Excel打开时,单元格中的文本自动换行?
I recommend you to use the Excel Library for this kind of task. 我建议您将Excel库用于此类任务。 Has a pretty good result!
效果不错!
https://code.google.com/p/excellibrary/ https://code.google.com/p/excellibrary/
与其在Style中执行WrapText,不如尝试在Alignment中执行:
sb.AppendFormat(@" <Alignment ss:WrapText=""1"" ss:Vertical=""Bottom""/>{0}", Environment.NewLine);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.