domenica 31 agosto 2008

Fibonacci numbers

Beh, quanto {C}bonacci non potevo no fare un riferimento al mio avo!?! scherzo :-)

  145 //...I primi 41 numeri di Fibonacci sono:

  146 //...0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 (=F10),

  147 //...89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765 (=F20),

  148 //...

  149 Func<int, int> _fibo = null;

  150 _fibo = n => n > 1 ? _fibo(n - 1) + _fibo(n - 2) : n;

  151 Console.WriteLine( _fibo(20) );


Ciao.

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

mercoledì 27 agosto 2008

Extract only numbers from a String in SQL Server

Circa una settimana fa, davanti ad un buon caffè, Guglielmo ovviamente :-), un mio carissimo amico mi chiese una Function in SQL Server che estraesse da una stringa solo numeri.
Lavorando da molti anni in Oracle ho subito pensato alle functions Translate or regexp_replace (10g).
Ma poi mi sono ricordato che in SQL Server, la funzione Translate non esiste e l’utilizzo delle “Regular Expression” è possibile sono by CLR .

Allora documentandomi un po’ in giro ecco una soluzione carina, spero!?! :-)

declare @in_string varchar(255)
declare @out_string varchar(255)

set @in_string='Carlopoli21082008Claudio&Rino&pureSergio'
set @out_string = ''


select @out_string = @out_string + a.sub_char
from
(

select substring(@in_string, b.number,1) as sub_char
from master..spt_values b
where b.type = 'p'
and b.number between 1 and len(@in_string)
) as a
where isnumeric(a.sub_char) = 1


print @out_string

Ciao.
PS: Ringrazio il "vecchio", ma sempre saggio, Arcuri ... :-)

sabato 23 agosto 2008

Birth!!!


... dopo 2 settimane di totale relax a Carlopoli :-) nasce qui questo nuovo blog!