Example #1
0
int OGRXLSDataSource::Open( const char * pszFilename, int bUpdateIn)

{
    if (bUpdateIn)
    {
        return FALSE;
    }

#ifdef _WIN32
    if( CPLTestBool( CPLGetConfigOption( "GDAL_FILENAME_IS_UTF8", "YES" ) ) )
        pszName = CPLRecode( pszFilename, CPL_ENC_UTF8, CPLString().Printf( "CP%d", GetACP() ) );
    else
        pszName = CPLStrdup( pszFilename );
#else
    pszName = CPLStrdup( pszFilename );
#endif

// --------------------------------------------------------------------
//      Does this appear to be a .xls file?
// --------------------------------------------------------------------

    /* Open only for getting info. To get cell values, we have to use freexl_open */
    if (freexl_open_info (pszName, &xlshandle) != FREEXL_OK)
        return FALSE;

    unsigned int nSheets = 0;
    if (freexl_get_info (xlshandle, FREEXL_BIFF_SHEET_COUNT, &nSheets) != FREEXL_OK)
        return FALSE;

    for(unsigned short i=0; i<(unsigned short)nSheets; i++)
    {
        freexl_select_active_worksheet(xlshandle, i);

        const char* pszSheetname = NULL;
        if (freexl_get_worksheet_name(xlshandle, i, &pszSheetname) != FREEXL_OK)
            return FALSE;

        unsigned int nRows = 0;
        unsigned short nCols = 0;
        if (freexl_worksheet_dimensions(xlshandle, &nRows, &nCols) != FREEXL_OK)
            return FALSE;

        /* Skip empty sheets */
        if (nRows == 0)
            continue;

        papoLayers = (OGRLayer**) CPLRealloc(papoLayers, (nLayers + 1) * sizeof(OGRLayer*));
        papoLayers[nLayers ++] = new OGRXLSLayer(this, pszSheetname, i, (int)nRows, nCols);
    }

    freexl_close(xlshandle);
    xlshandle = NULL;

    return TRUE;
}
Example #2
0
OGRXLSDataSource::~OGRXLSDataSource()

{
    for( int i = 0; i < nLayers; i++ )
        delete papoLayers[i];
    CPLFree( papoLayers );

    CPLFree( pszName );

    if (xlshandle)
        freexl_close(xlshandle);
}
void FreeXL_Close(sLONG_PTR *pResult, PackagePtr pParams)
{
    C_LONGINT Param1;
    C_LONGINT returnValue;
    
    Param1.fromParamAtIndex(pParams, 1);
    int error = FREEXL_NULL_HANDLE;
    
    xls_handle h = __handleGet(Param1.getIntValue());
    if(h){
        error = freexl_close(h);         
        if(FREEXL_OK == error){
            __handleClear(Param1.getIntValue());
        }
    }
    
    returnValue.setIntValue(error);    
    returnValue.setReturn(pResult);
}
Example #4
0
int load_excel(char *filename)
{
	FreeXL_CellValue cell;
	const void *handle;
    unsigned int info;
    unsigned int rows;
    unsigned short columns;
    unsigned int row;
    unsigned short col;
	int ret;
	int worksheet_index = 0;

	memset(name, 0, sizeof(name));
	memset(data, 0, sizeof(data));	

    ret = freexl_open (filename, &handle);
    if (ret != FREEXL_OK)
	{
		fprintf (stderr, "OPEN ERROR: %d\n", ret);
		return -1;
	}

    ret = freexl_get_info (handle, FREEXL_BIFF_PASSWORD, &info);
    if (ret != FREEXL_OK)
	{
		fprintf (stderr, "GET-INFO [FREEXL_BIFF_PASSWORD] Error: %d\n", ret);
		goto stop;
	}
    switch (info)
	{
		case FREEXL_BIFF_PLAIN:
			break;
		case FREEXL_BIFF_OBFUSCATED:
		default:
			fprintf (stderr, "Password protected: (not accessible)\n");
			goto stop;
	};

	ret = freexl_select_active_worksheet (handle, worksheet_index);
	if (ret != FREEXL_OK)
	{
		fprintf (stderr, "SELECT-ACTIVE_WORKSHEET Error: %d\n", ret);
		goto stop;
	}
			/* dimensions */
	ret = freexl_worksheet_dimensions (handle, &rows, &columns);
	if (ret != FREEXL_OK)
	{
		fprintf (stderr, "WORKSHEET-DIMENSIONS Error: %d\n", ret);
		goto stop;
	}

	if (rows < 3)
	{
		fprintf (stderr, "至少需要三行: %d\n", ret);
		goto stop;		
	}

	max_columns = columns;
	max_rows = rows;

	row = 1;

	for (col = 0; col < columns; col++)
	{
		ret = freexl_get_cell_value (handle, row, col, &cell);
		if (ret != FREEXL_OK)
		{
			fprintf (stderr,
				"CELL-VALUE-ERROR (r=%u c=%u): %d\n", row,
				col, ret);
			goto stop;
		}
		if (cell.type != FREEXL_CELL_TEXT
			&& cell.type != FREEXL_CELL_SST_TEXT) {
			name[col] = strdup("NOT__ALREADY__SET");
		} else
			name[col] = strdup(cell.value.text_value);
	}
	
	for (row = 2; row < rows; row++)
	{
		for (col = 0; col < columns; col++)
		{
			ret = freexl_get_cell_value (handle, row, col, &cell);
			if (ret != FREEXL_OK)
			{
				fprintf (stderr,
					"CELL-VALUE-ERROR (r=%u c=%u): %d\n", row,
					col, ret);
				goto stop;
			}
			switch (cell.type)
			{
				case FREEXL_CELL_INT:
//					printf (", %d", cell.value.int_value);
					sprintf(tmpvalue, "%d", cell.value.int_value);
					data[row - 2][col] = strdup(tmpvalue);
					break;
				case FREEXL_CELL_DOUBLE:
//					printf (", %1.12f", cell.value.double_value);
					sprintf(tmpvalue, "%d", (int)cell.value.double_value);
					data[row - 2][col] = strdup(tmpvalue);					
					break;
				case FREEXL_CELL_TEXT:
				case FREEXL_CELL_SST_TEXT:
//					print_sql_string (cell.value.text_value);
//					printf (", '%s'", cell.value.text_value);
					data[row - 2][col] = strdup(cell.value.text_value);							
					break;
				case FREEXL_CELL_DATE:
				case FREEXL_CELL_DATETIME:
				case FREEXL_CELL_TIME:
//					printf (", '%s'", cell.value.text_value);
					data[row - 2][col] = strdup(cell.value.text_value);					
					break;
				case FREEXL_CELL_NULL:
				default:
//					printf (", NULL");
					data[row - 2][col] = NULL;
					break;
			};
		}
	}
	return (0);
stop:
/* closing the .XLS file [Workbook] */
    ret = freexl_close (handle);
    if (ret != FREEXL_OK)
	{
		fprintf (stderr, "CLOSE ERROR: %d\n", ret);
		return -1;
	}

	return (0);
}
Example #5
0
int load_map_excel(char *filename)
{
	FreeXL_CellValue cell;
	const void *handle;
    unsigned int info;
    unsigned int rows;
    unsigned short columns;
    unsigned int row;
    unsigned short col;
	int ret;
	int worksheet_index = 0;

    ret = freexl_open (filename, &handle);
    if (ret != FREEXL_OK)
	{
		fprintf (stderr, "OPEN ERROR: %d\n", ret);
		return -1;
	}

    ret = freexl_get_info (handle, FREEXL_BIFF_PASSWORD, &info);
    if (ret != FREEXL_OK)
	{
		fprintf (stderr, "GET-INFO [FREEXL_BIFF_PASSWORD] Error: %d\n", ret);
		goto stop;
	}
    switch (info)
	{
		case FREEXL_BIFF_PLAIN:
			break;
		case FREEXL_BIFF_OBFUSCATED:
		default:
			fprintf (stderr, "Password protected: (not accessible)\n");
			goto stop;
	};

	ret = freexl_select_active_worksheet (handle, worksheet_index);
	if (ret != FREEXL_OK)
	{
		fprintf (stderr, "SELECT-ACTIVE_WORKSHEET Error: %d\n", ret);
		goto stop;
	}
			/* dimensions */
	ret = freexl_worksheet_dimensions (handle, &rows, &columns);
	if (ret != FREEXL_OK)
	{
		fprintf (stderr, "WORKSHEET-DIMENSIONS Error: %d\n", ret);
		goto stop;
	}

	if (columns >= MAPROW || rows >= MAPCOL)
	{
		fprintf (stderr, " Error: map is too big %d %d\n", columns, row);
		goto stop;		
	}

	for (row = 0; row < rows; row++)
	{
		for (col = 0; col < columns; col++)
		{
			ret = freexl_get_cell_value (handle, row, col, &cell);
			if (ret != FREEXL_OK)
			{
				fprintf (stderr,
					"CELL-VALUE-ERROR (r=%u c=%u): %d\n", row,
					col, ret);
				goto stop;
			}
			switch (cell.type)
			{
				case FREEXL_CELL_INT:
//					printf (", %d", cell.value.int_value);
					TestMap[row][col] = cell.value.int_value;
					break;
				case FREEXL_CELL_DOUBLE:
//					printf (", %1.12f", cell.value.double_value);
					TestMap[row][col] = cell.value.double_value;					
					break;
				default:
//					printf (", NULL");
					TestMap[row][col] = 0;
					break;
			};
		}
	}
	return (0);
stop:
/* closing the .XLS file [Workbook] */
    ret = freexl_close (handle);
    if (ret != FREEXL_OK)
	{
		fprintf (stderr, "CLOSE ERROR: %d\n", ret);
		return -1;
	}

	return (0);
}