Tôi đang xuất dữ liệu vào Excel từ một trang web. Đây không phải là không có trí tuệ, nhưng có <p>
thẻ trong dữ liệu. Điều này làm cho Excel tạo các hàng mới khi dữ liệu tất cả nên ở trong cùng một ô. Sau một số nghiên cứu tôi thấy rằng vị trí mso-data nên thực hiện thủ thuật, nhưng nó không hoạt động. Excel mở ra, dữ liệu được hiển thị, nhưng các hàng thừa không cần thiết được tạo ra. Dưới đây là đoạn code tôi sử dụng để xuất dữ liệu:mso-data-placement: cùng một ô không hoạt động
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();
}
}
}
Đây là một ví dụ về các dữ liệu thô trong cơ sở dữ liệu đó là đem lại cho tôi đau buồn:
<P>4/13/2011 : Cheng "Jonathan" Vaing is with BSES Graffiti Unit.</P><P>4/13/2011 : Cheng "Jonathan" Vaing is with</P>
Gợi ý?
Tôi đã thử một vài điều khác
- tôi đã đi thẳng đến dữ liệu và thêm thuộc tính mso-dữ liệu vị trí cho thẻ đoạn inline. Vẫn không hoạt động. Các dữ liệu trông như thế này
<P style="mso-data-placement:same-cell> my data </p>
- tôi đã cố gắng mso- thuộc tính * khác, điều đó không làm việc, hoặc. Ví dụ, tôi đã thay đổi kiểu của tôi trông như thế này
<style type='text/css'>P {mso-highlight:yellow}</style>";
Tại sao oh lý do tại sao không Excel nhận ra thuộc tính * mso- của tôi?!?!