我正在将数据从网页导出到 Excel。这应该不费吹灰之力,但<p>
数据中有标签。当数据应该都在同一个单元格中时,这会导致 Excel 创建新行。经过一些研究,我发现 mso-data-placement 应该可以解决问题,但它不起作用。Excel 打开,显示数据,但创建了多余的行。这是我用来导出数据的代码:
protected void doexcel()
{
string style = @"<style type='text/css'>P {mso-data-placement:same-cell; font-weight:bold;}</style>";
HttpResponse response = HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
//set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
Random RandomClass = new Random();
int RandomNumber = RandomClass.Next();
String filename = "a" + RandomNumber + DateTime.Now + ".xls";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"" );
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
HttpContext.Current.Response.Write(style);
SqlDataSourceEmployeeAssets.ConnectionString = MyObjects.Application.CurrentContext.ConnectionString;
String sql = (string)Session["sql"];
SqlDataSourceEmployeeAssets.SelectCommand = sql;
// lCount.Text = "Query returned " + getCount(query) + " rows.";
DataGrid dge = new DataGrid();
dge.DataSource = SqlDataSourceEmployeeAssets;
dge.DataBind();
dge.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
这是数据库中让我感到悲痛的原始数据示例:
<P>4/13/2011 : Cheng "Jonathan" Vaing is with BSES Graffiti Unit.</P><P>4/13/2011 : Cheng "Jonathan" Vaing is with</P>
建议?
我尝试了其他几件事
- 我直接进入数据并将 mso-data-placement 属性添加到内联的段落标记中。仍然没有工作。数据看起来像这样
<P style="mso-data-placement:same-cell> my data </p>
- 我尝试了其他 mso-* 属性,但也没有用。例如,我将样式表更改为如下所示
<style type='text/css'>P {mso-highlight:yellow}</style>";
为什么哦为什么 Excel 不能识别我的 mso-* 属性?!?!