domenica 31 agosto 2008

Generate and send by mail an Excel File using Office Open XML spreadsheet data format

Qualche tempo fa ho avuto l'esigenza di siluppare una funzione che generasse un Excel File e lo inviasse come "attach" via email.
Questo il codice:

   14 /// <summary>

   15 /// Create a E-Mail Attach from Stream

   16 /// </summary>

   17 private static void StreamToAttachment()

   18 {

   19     //...create the mail message

   20     MailMessage _mail = new MailMessage();

   21 

   22     //...set the addresses and content

   23     _mail.From = new MailAddress("mailFrom@cbonacci.net");

   24     _mail.To.Add("mailFrom@cbonacci.net");

   25     _mail.Subject = "Excel: Attachments and Streams";

   26     _mail.Body = "... I love C# ...!";

   27 

   28     //... retrieve binary data and save to a stream

   29     byte[] _data = _streamBuilder();

   30     MemoryStream _stream = new MemoryStream(_data);

   31 

   32     _mail.Attachments.Add(

   33                         new Attachment(_stream,

   34                             "cb_example.xls",

   35                             MediaTypeNames.Text.Plain)

   36                            );

   37 

   38     //...send the message

   39     SmtpClient _smtp = new SmtpClient("127.0.0.1");

   40     _smtp.Send(_mail);

   41 }

   42 

   43 /// <summary>

   44 ///

   45 /// </summary>

   46 /// <returns></returns>

   47 private static byte[] _streamBuilder()

   48 {

   49     string _xml = _createWorkbookFromdataSet(

   50                         //...get your data from dataBase

   51                         GetDataForDB() );

   52 

   53     byte[] _byteData = Encoding.ASCII.GetBytes(_xml);

   54     return _byteData;

   55 }

   56 

   57 /// <summary>

   58 ///

   59 /// </summary>

   60 /// <param name="sourceData"></param>

   61 /// <returns></returns>

   62 private static string _createWorkbookFromdataSet(DataSet sourceData)

   63 {

   64     XmlDataDocument _xmlDataDoc =

   65                         new XmlDataDocument(sourceData);

   66     XslCompiledTransform _xt =

   67                         new XslCompiledTransform();

   68     StreamReader _reader = new

   69 

   70     StreamReader("Excel.xsl");

   71     XmlTextReader xRdr = new XmlTextReader(_reader);

   72 

   73     _xt.Load(xRdr, null, null);

   74     StringWriter _sw = new StringWriter();

   75     _xt.Transform(_xmlDataDoc, null, _sw);

   76 

   77     //...retunr string XML Worksheet.

   78     return _sw.ToString();

   79 }


... a very simple Excel/xls

    1 <xsl:stylesheet version="1.0"

    2     xmlns="urn:schemas-microsoft-com:office:spreadsheet"

    3     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    4  xmlns:msxsl="urn:schemas-microsoft-com:xslt"

    5  xmlns:user="urn:my-scripts"

    6  xmlns:o="urn:schemas-microsoft-com:office:office"

    7  xmlns:x="urn:schemas-microsoft-com:office:excel"

    8  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >

    9 

   10 <xsl:template match="/">

   11   <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"

   12     xmlns:o="urn:schemas-microsoft-com:office:office"

   13     xmlns:x="urn:schemas-microsoft-com:office:excel"

   14     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"

   15     xmlns:html="http://www.w3.org/TR/REC-html40">

   16     <xsl:apply-templates/>

   17   </Workbook>

   18 </xsl:template>

   19 

   20 <xsl:template match="/*">

   21   <Worksheet>

   22   <xsl:attribute name="ss:Name">

   23   <xsl:value-of select="local-name(/*/*)"/>

   24   </xsl:attribute>

   25     <Table x:FullColumns="1" x:FullRows="1">

   26       <Row>

   27         <xsl:for-each select="*[position() = 1]/*">

   28           <Cell><Data ss:Type="String">

   29           <xsl:value-of select="local-name()"/>

   30           </Data></Cell>

   31         </xsl:for-each>

   32       </Row>

   33       <xsl:apply-templates/>

   34     </Table>

   35   </Worksheet>

   36 </xsl:template>

   37 

   38 <xsl:template match="/*/*">

   39   <Row>

   40     <xsl:apply-templates/>

   41   </Row>

   42 </xsl:template>

   43 

   44 <xsl:template match="/*/*/*">

   45   <Cell><Data ss:Type="String">

   46     <xsl:value-of select="."/>

   47   </Data></Cell>

   48 </xsl:template>


Ciao

0 commenti: