[Programmazione] Leggere file xls con PHP

October 20th, 2009 Giulio Rossetti Posted in HowTo, OpenSource, Programmazione No Comments »

No Gravatar

Come buona parte dei miei post anche questo nasce, oltre che con l’intento di fare un po’ di informazione, per ricordarmi di come ho già risolto un problema che mi si è presentato: in effetti la mia memoria di solito è fallace e googolare per cercar la stessa informazione più di una volta non mi piace granchè ^_^

Un attività che spesso capita di dover effettuare è quella di processare “on the fly” un file xls (magari disponibile a seguito dell’upload di un utente) e gestire i dati in esso contenuti (possibilmente strutturati come da noi previsto altrimenti la cosa è improponibile..) per, ad esempio, popolare tabelle di un database o più semplicemente visualizzarli sul nostro sito.

Bene, la soluzione a questo problema è più semplice di quanto si pensi.

Esiste infatti un progetto su Sourceforge che tramite la classe ExcelReader rende disponibile tutto ciò che ci occorre.

Dopo aver incluso i due file reader.php e oleread.inc nel nostro script e saremo quindi pronti per svolgere il lavoro “sporco”.

Per prima cosa inizializziamo la nostra classe  e in seguito passiamogli il file da aprire:

$data->read('nome_filel.xls');

la cosa successiva da fare, ovviamente, è effettuare un ciclo per ogni riga e colonna ricordando che:

$data->sheets[0]['numRows'];
$data->sheets[0]['numCols'];

rappresentano rispettivamente il numero di righe e colonne contenute nel file xls (in particolare nel primo foglio del documento).
A questo punto possiamo prelevare il valore contenuto in ogni cella accedendovi come faremo con una banalissima matrice:

$data->sheets[0]['cells'][$i][$j];

Un esempio di script per mostrare a video i dati di un file xls in forma tabellare è quindi il seguente:

<?
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read('nome_file.xls');
error_reporting(E_ALL ^ E_NOTICE);

echo "<table>";
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
        echo "<tr>";
	for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
		echo "<td>".$data->sheets[0]['cells'][$i][$j]."</td>";
	}
	echo "</tr>";
}
echo "</table>";
?>

PS: Ovviamente è possibile ciclare anche sui fogli che compongono il file xls..

Reblog this post [with Zemanta]

Popularity: 8% [?]

AddThis Social Bookmark Button

Chrome OS annunciato per la seconda metà del 2010

July 8th, 2009 Giulio Rossetti Posted in Internet, OpenSource, Pensieri 4 Comments »

No Gravatar
Google Chrome
Image via Wikipedia

In alcuni casi è meglio non limitarsi a fornire informazioni di seconda mano, per questo riporto il testo integrale del post apparso ieri sull’Official Google Blog dal titolo Introducing the Google Chrome OS

It’s been an exciting nine months since we launched the Google Chrome browser. Already, over 30 million people use it regularly. We designed Google Chrome for people who live on the web — searching for information, checking email, catching up on the news, shopping or just staying in touch with friends. However, the operating systems that browsers run on were designed in an era where there was no web. So today, we’re announcing a new project that’s a natural extension of Google Chrome — the Google Chrome Operating System. It’s our attempt to re-think what operating systems should be.

Google Chrome OS is an open source, lightweight operating system that will initially be targeted at netbooks. Later this year we will open-source its code, and netbooks running Google Chrome OS will be available for consumers in the second half of 2010. Because we’re already talking to partners about the project, and we’ll soon be working with the open source community, we wanted to share our vision now so everyone understands what we are trying to achieve.

Speed, simplicity and security are the key aspects of Google Chrome OS. We’re designing the OS to be fast and lightweight, to start up and get you onto the web in a few seconds. The user interface is minimal to stay out of your way, and most of the user experience takes place on the web. And as we did for the Google Chrome browser, we are going back to the basics and completely redesigning the underlying security architecture of the OS so that users don’t have to deal with viruses, malware and security updates. It should just work.

Google Chrome OS will run on both x86 as well as ARM chips and we are working with multiple OEMs to bring a number of netbooks to market next year. The software architecture is simple — Google Chrome running within a new windowing system on top of a Linux kernel. For application developers, the web is the platform. All web-based applications will automatically work and new applications can be written using your favorite web technologies. And of course, these apps will run not only on Google Chrome OS, but on any standards-based browser on Windows, Mac and Linux thereby giving developers the largest user base of any platform.

Google Chrome OS is a new project, separate from Android. Android was designed from the beginning to work across a variety of devices from phones to set-top boxes to netbooks. Google Chrome OS is being created for people who spend most of their time on the web, and is being designed to power computers ranging from small netbooks to full-size desktop systems. While there are areas where Google Chrome OS and Android overlap, we believe choice will drive innovation for the benefit of everyone, including Google.

We hear a lot from our users and their message is clear — computers need to get better. People want to get to their email instantly, without wasting time waiting for their computers to boot and browsers to start up. They want their computers to always run as fast as when they first bought them. They want their data to be accessible to them wherever they are and not have to worry about losing their computer or forgetting to back up files. Even more importantly, they don’t want to spend hours configuring their computers to work with every new piece of hardware, or have to worry about constant software updates. And any time our users have a better computing experience, Google benefits as well by having happier users who are more likely to spend time on the Internet.

We have a lot of work to do, and we’re definitely going to need a lot of help from the open source community to accomplish this vision. We’re excited for what’s to come and we hope you are too. Stay tuned for more updates in the fall and have a great summer.

Sinceramente sono abbastanza “soddisfatto” da questa decisione di Google di entrare nel mercato dei SO (in modo open a quanto pare, e questo rinforza la mia soddisfazione). L’unico effetto negativo evidente è che Orwell quando scrisse 1984 non immaginava minimamente che il “Grande Fratello” potesse essere bene accolto dalla popolazione stessa.

Nulla da ridire sui servizi offerti da “Big G”, di cui sono un utilizzatore sconsiderato da tempo, ma effettivamente c’è da riconoscere che al giorno d’oggi le informazioni in mano a tale azienda sono talmente tante e talmente capillari che chiunque di noi può considerarsi “schedato e\o schedabile” senza troppi problemi.

Ai posteri l’ardua sentenza.. intanto aspettiamo per valutare la portata effettiva del progetto..

Reblog this post [with Zemanta]

Popularity: 6% [?]

AddThis Social Bookmark Button

Ubuntu One finalmente è arrivato l’invito!!

July 1st, 2009 Giulio Rossetti Posted in HowTo, Internet, Linux, Software, Ubuntu 2 Comments »

No Gravatar

Ormai avevo perso le speranze (avevo fatto richiesta  diversi mesi fa’ proprio nei primi giorni in cui il servizio offerto da Canonical fu lanciato sul web) ma finalmente stamattina una mail di invito è arrivata ^_^

UbuntuOne, come è già stato ripetuto a gran voce nella blogosfera al momento del lancio del servizio, si propone come un semplice strumento per la sincronizzazione tra più computer.

Al servizio al momento si accede solo su invito (che può essere richiesto al sito del ufficiale): ricevuto lo stesso è quindi possibile installare il client dal repository che viene indicato e registrare i computer da abilitare al servizio sul proprio account (utilizza per l’accesso l’account di lauchpad).

Per le poche prove che ho fatto negli ultimi minuti mi sembra un ottimo servizio che, volendo, potrebbe essere esteso con ulteriori funzionalità.

Avviato il client ci è sufficiente copiare i file desiderati nella cartella “Ubuntu One” creata nella nostra home e attendere la sincronizzazione con lo spazio remoto – nulla di più semplice ed immediato. Unico neo è il poco spazio a disposizione nella versione gratuita del servizio

Il servizio è offerto infatti con 2 piani tariffari: il piano base gratuito (2Gb di spazio) e quello a pagamento (10$ al mese per 10GB di spazio).

Popularity: 8% [?]

AddThis Social Bookmark Button