Sto esportando i dati in Excel da una pagina web. Questo dovrebbe essere un gioco da ragazzi, ma ci sono i tag <p>
nei dati. Ciò causa che Excel crea nuove righe quando i dati dovrebbero essere tutti nella stessa cella. Dopo alcune ricerche ho scoperto che il posizionamento di mso-data dovrebbe fare il trucco, ma non funziona. Excel si apre, i dati vengono visualizzati, ma vengono create altre righe non necessarie. Qui è il codice che uso per esportare i dati:mso-data-placement: stessa cella non funzionante
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();
}
}
}
Questo è un esempio dei dati grezzi nel database che mi sta dando il dolore:
<P>4/13/2011 : Cheng "Jonathan" Vaing is with BSES Graffiti Unit.</P><P>4/13/2011 : Cheng "Jonathan" Vaing is with</P>
Suggerimenti?
Ho provato un paio di altre cose
- sono andato dritto ai dati e ha aggiunto l'attributo mso-dati-placement al paragrafo tag in linea. Ancora non ha funzionato. I dati sembravano questo
<P style="mso-data-placement:same-cell> my data </p>
- ho provato altri attributi mso- *, che non ha funzionato neanche. Per esempio, ho cambiato il mio foglio di stile per assomigliare a questo
<style type='text/css'>P {mso-highlight:yellow}</style>";
Perché oh perché non riconosce Excel miei attributi mso- *?!?!