6

我正在将数据从网页导出到 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>

建议?


我尝试了其他几件事

  1. 我直接进入数据并将 mso-data-placement 属性添加到内联的段落标记中。仍然没有工作。数据看起来像这样

<P style="mso-data-placement:same-cell> my data </p>

  1. 我尝试了其他 mso-* 属性,但也没有用。例如,我将样式表更改为如下所示

<style type='text/css'>P {mso-highlight:yellow}</style>";

为什么哦为什么 Excel 不能识别我的 mso-* 属性?!?!

4

1 回答 1

1

There is a solution but it is not clean.

After the dge.DataBind, place the following code. This will encode the text of each cell

 foreach (DataGridItem dgi in dge.Items)
 {                
      foreach (TableCell cell in dgi.Cells)
      {
           cell.Text = WebUtility.HtmlEncode(cell.Text);;
      }
 }        

The Excel file, when opened, should show the raw data with the markup, all in one cell.

I found that this works because Excel actually encodes the text, as well. To see what Excel does in action, do the following:

  1. Create a new workbook in Excel (I am using Office 2013).
  2. In the first cell, paste the raw data (as you have it displayed). Do this by first pressing F2 (insert into cell), then paste the text.
  3. Save the workbook as an HTML file (or web page).
  4. Using windows explorer, go to the folder location of where you saved the file. There should be a hidden folder (i think it is hidden) with the same name as your file. For example, if your workbook is Book1.htm, there should be a folder labeled Book1_files.
  5. In this folder, there should be an HTM file with the name sheet001.htm. Open this file in notepad (or any text editor...not excel or word)
  6. Locate your raw data. You will see that the text is not showing the HTML markup, rather it is showing the encoded version.

Hope this helps.

于 2013-12-17T17:08:39.300 回答