Hello,

Talking with Ben I decide to try to make an excel data import to pspp.
I've found libxls (http://libxls.sourceforge.net/) an LGPL3
library to help read this kind of file.

I was able to make a small program that open an excel file and returns
it as a csv. It would be very nice if you could help me to
improve/plug it into
pspp. Where do I start?

Regards,

Michel

-- 
Michel Almada de Castro Boaventura
Graduando em Ciência da Computação pela UFMG
PSPP, um substituto livre do SPSS - pspp.michelboaventura.com
Twitter: twitter.com/michelgbr
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxls/xls.h>

/* link this against libxlsreader */

/* Print a string */
void OutputString(const char *string) {
	const char *str;
  const char sep = '"';

	printf("%c", sep);

	for (str = string; *str; str++)
  {
		if (*str == sep)
			printf("\\%c", sep);
    else if (*str == '\\') {
			printf("\\\\");
		} else {
			printf("%c", *str);
		}
	}
	printf("%c", sep);
}

void
print_cell(xlsCell *cell)
{
  /* Numeric */
  if (cell->id == 0x27e || cell->id == 0x0BD || cell->id == 0x203)
    printf("%.15g", cell->d);

  /* Formula */
  else if (cell->id == 0x06)
  {
    /* Numeric Formula */
    if (cell->l == 0)
       printf("%.15g", cell->d);
    else
    {
      /* Boolean Formula */
      if (strcmp(cell->str,"bool"))
        OutputString((int) cell->d ? "true" : "false");
      /* Error Formula */
      else if (strcmp(cell->str,"error"))
        OutputString("*error*");
      /* Probably a String Formula */
      else
        OutputString(cell->str);
    }
  }
  /* String? */
  else if (cell->str != NULL)
    OutputString(cell->str);
  /*Empty Cell*/
  else
    OutputString("");
}

int
main()
{
  char *filename = "test.xls";
  xlsWorkBook* pWB;
  xlsWorkSheet* pWS;
  int i,j,num_rows,num_cols;

  /* Open the workbook */
  pWB = xls_open(filename, "ASCII");

  /* Open the first sheet and parse it.
   * don't care about the rest yet */
  pWS = xls_getWorkSheet(pWB, 0);
  xls_parseWorkSheet(pWS);

  num_rows = pWS->rows.lastrow;
  num_cols = pWS->rows.lastcol;

  for (i = 0; i <= num_rows; i++)
  {
    for (j = 0; j <= num_cols; j++)
    {
      xlsCell *cell = xls_cell(pWS, i, j);

      /*Ignore missing or invisibles cells*/
      if ((!cell) || (cell->ishiden)) continue;
      print_cell(cell);

      /* Don't print a comma after last cel */
      if (j < num_cols - 1) printf(",");
    }
    printf("\n");
  }
  return 0;
}

_______________________________________________
pspp-dev mailing list
pspp-dev@gnu.org
http://lists.gnu.org/mailman/listinfo/pspp-dev

Reply via email to