void OGRXLSLayer::DetectHeaderLine(const void* xlshandle) { FreeXL_CellValue sCellValue; int nCountTextOnSecondLine = 0; unsigned short i = 0; // Used after for. for( ; i < nCols && nRows >= 2; i++ ) { if (freexl_get_cell_value(xlshandle, 0, i, &sCellValue) == FREEXL_OK) { if (sCellValue.type != FREEXL_CELL_TEXT && sCellValue.type != FREEXL_CELL_SST_TEXT) { /* If the values in the first line are not text, then it is */ /* not a header line */ break; } } if (freexl_get_cell_value(xlshandle, 1, i, &sCellValue) == FREEXL_OK) { if (sCellValue.type == FREEXL_CELL_TEXT || sCellValue.type == FREEXL_CELL_SST_TEXT) { /* If there are only text values on the second line, then we cannot */ /* know if it is a header line or just a regular line */ nCountTextOnSecondLine ++; } } } const char* pszXLSHeaders = CPLGetConfigOption("OGR_XLS_HEADERS", ""); if( EQUAL(pszXLSHeaders, "FORCE") ) bFirstLineIsHeaders = true; else if( EQUAL(pszXLSHeaders, "DISABLE") ) bFirstLineIsHeaders = false; else if( i == nCols && nCountTextOnSecondLine != nCols ) bFirstLineIsHeaders = true; }
OGRFeature *OGRXLSLayer::GetNextRawFeature() { if (nNextFID == nRows) return nullptr; const void* xlshandle = poDS->GetXLSHandle(); if (xlshandle == nullptr) return nullptr; freexl_select_active_worksheet(xlshandle, (unsigned short)iSheet); OGRFeature* poFeature = new OGRFeature(poFeatureDefn); FreeXL_CellValue sCellValue; for(unsigned short i=0;i<(unsigned short )poFeatureDefn->GetFieldCount(); i++) { if (freexl_get_cell_value(xlshandle, nNextFID, i, &sCellValue) == FREEXL_OK) { switch (sCellValue.type) { case FREEXL_CELL_INT: poFeature->SetField(i, sCellValue.value.int_value); break; case FREEXL_CELL_DOUBLE: poFeature->SetField(i, sCellValue.value.double_value); break; case FREEXL_CELL_TEXT: case FREEXL_CELL_SST_TEXT: poFeature->SetField(i, sCellValue.value.text_value); break; case FREEXL_CELL_DATE: case FREEXL_CELL_DATETIME: case FREEXL_CELL_TIME: poFeature->SetField(i, sCellValue.value.text_value); break; case FREEXL_CELL_NULL: break; default: CPLDebug("XLS", "Unknown cell type = %d", sCellValue.type); break; } } } poFeature->SetFID(nNextFID + 1); nNextFID ++; return poFeature; }
void FreeXL_Get_cell_value(sLONG_PTR *pResult, PackagePtr pParams) { C_LONGINT Param1; C_LONGINT Param2; C_LONGINT Param3; C_LONGINT Param4; C_REAL Param5; C_TEXT Param6; C_LONGINT returnValue; Param1.fromParamAtIndex(pParams, 1); Param2.fromParamAtIndex(pParams, 2); Param3.fromParamAtIndex(pParams, 3); int error = FREEXL_NULL_HANDLE; xls_handle h = __handleGet(Param1.getIntValue()); if(h){ FreeXL_CellValue value; error = freexl_get_cell_value(h, Param2.getIntValue() -1, Param3.getIntValue() -1, &value); if(FREEXL_OK == error){ CUTF8String v; switch (value.type){ case FREEXL_CELL_INT: Param4.setIntValue(value.value.int_value); break; case FREEXL_CELL_DOUBLE: Param5.setDoubleValue(value.value.double_value); break; case FREEXL_CELL_TEXT: case FREEXL_CELL_SST_TEXT: case FREEXL_CELL_DATE: case FREEXL_CELL_DATETIME: case FREEXL_CELL_TIME: v = CUTF8String((const uint8_t *)value.value.text_value); Param6.setUTF8String(&v); break; case FREEXL_CELL_NULL: break; } } } Param4.toParamAtIndex(pParams, 4); Param5.toParamAtIndex(pParams, 5); Param6.toParamAtIndex(pParams, 6); returnValue.setIntValue(error); returnValue.setReturn(pResult); }
OGRFeatureDefn * OGRXLSLayer::GetLayerDefn() { if (poFeatureDefn) return poFeatureDefn; poFeatureDefn = new OGRFeatureDefn( pszName ); poFeatureDefn->Reference(); poFeatureDefn->SetGeomType( wkbNone ); const void* xlshandle = poDS->GetXLSHandle(); if (xlshandle == nullptr) return poFeatureDefn; freexl_select_active_worksheet(xlshandle, (unsigned short)iSheet); if (nRows > 0) { FreeXL_CellValue sCellValue; DetectHeaderLine(xlshandle); int* paeFieldTypes = (int* ) CPLMalloc(nCols * sizeof(int)); for( unsigned short i = 0; i < nCols; i++ ) { paeFieldTypes[i] = -1; } const char* pszXLSFieldTypes = CPLGetConfigOption("OGR_XLS_FIELD_TYPES", ""); if (!EQUAL(pszXLSFieldTypes, "STRING")) DetectColumnTypes(xlshandle, paeFieldTypes); for( unsigned short i = 0; i < nCols; i++ ) { OGRFieldType eType = (OGRFieldType) paeFieldTypes[i]; if (paeFieldTypes[i] < 0) eType = OFTString; if( bFirstLineIsHeaders && freexl_get_cell_value(xlshandle, 0, i, &sCellValue) == FREEXL_OK && (sCellValue.type == FREEXL_CELL_TEXT || sCellValue.type == FREEXL_CELL_SST_TEXT) ) { OGRFieldDefn oField(sCellValue.value.text_value, eType); poFeatureDefn->AddFieldDefn(&oField); } else { OGRFieldDefn oField(CPLSPrintf("Field%d", i+1), eType); poFeatureDefn->AddFieldDefn(&oField); } } CPLFree(paeFieldTypes); } ResetReading(); return poFeatureDefn; }
void OGRXLSLayer::DetectColumnTypes(const void* xlshandle, int* paeFieldTypes) { FreeXL_CellValue sCellValue; for( int j = bFirstLineIsHeaders ? 1 : 0; j < nRows; j++ ) { for( unsigned short i = 0; i < nCols; i ++) { if (freexl_get_cell_value(xlshandle, j, i, &sCellValue) == FREEXL_OK) { int eType = paeFieldTypes[i]; switch (sCellValue.type) { case FREEXL_CELL_INT: eType = OFTInteger; break; case FREEXL_CELL_DOUBLE: eType = OFTReal; break; case FREEXL_CELL_TEXT: case FREEXL_CELL_SST_TEXT: eType = OFTString; break; case FREEXL_CELL_DATE: eType = OFTDate; break; case FREEXL_CELL_DATETIME: eType = OFTDateTime; break; case FREEXL_CELL_TIME: eType = OFTTime; break; case FREEXL_CELL_NULL: break; default: break; } if (paeFieldTypes[i] < 0) { paeFieldTypes[i] = eType; } else if (eType != paeFieldTypes[i]) { if ((paeFieldTypes[i] == OFTDate || paeFieldTypes[i] == OFTTime || paeFieldTypes[i] == OFTDateTime) && (eType == OFTDate || eType == OFTTime || eType == OFTDateTime)) paeFieldTypes[i] = OFTDateTime; else if (paeFieldTypes[i] == OFTReal && eType == OFTInteger) /* nothing */ ; else if (paeFieldTypes[i] == OFTInteger && eType == OFTReal) paeFieldTypes[i] = OFTReal; else paeFieldTypes[i] = OFTString; } } } } }
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); }
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); }