コード例 #1
0
void XLS2SQL::xls2SQL(string fileName)
{
    char t_fileName[fileName.length()+1];
    strncpy(t_fileName,fileName.c_str(),sizeof(t_fileName));
    t_fileName[sizeof(t_fileName)-1] = '\0';
    pxb = xls_open(t_fileName,"UTF-8");
    if(NULL == pxb)
    {
        cout<<"xls file open failed!";
    }

    pxs = xls_getWorkSheet(pxb,0);
    xls_parseWorkSheet(pxs);
    
    int rowNum = pxs->rows.lastrow;

    row0 = &pxs->rows.row[0];
    string colName = "";
    string cellContent = "";
    DSLFile df;
    int index = 0;
    for(r = 1;r <=rowNum;r++)
    {
        ++index;
        SymIpa sysIpa;
        row = &pxs->rows.row[r];
        int colNum = pxs->rows.lastcol;
        Data data;
        for(c = 0;c < colNum;c++)
        {
            colName = df.Pchar2Str(row0->cells.cell[c].str);
            cellContent = df.Pchar2Str(row->cells.cell[c].str);
            if(0 == strcmp(colName.c_str(),"SYM"))
            {
                sysIpa.SetSym(cellContent);
            }else if(0 == strcmp(colName.c_str(),"IPA"))
            {
                sysIpa.SetIpa(cellContent);
            }else if(0 == strcmp(colName.c_str(),"Example"))
            {
                sysIpa.SetExample(cellContent);
            }else if(0 == strcmp(colName.c_str(),"PhoneID"))
            {
                //cout<<stoi(cellContent)<<endl;
                sysIpa.SetId(index);
            }
        }
        data.InsertSymIpa(&sysIpa);

    }
    cout<<"所有行数据输出完毕"<<endl;
    xls_close_WS(pxs);
    xls_close_WB(pxb);
}
コード例 #2
0
ファイル: xls_reader.cpp プロジェクト: zheng39562/common
	void XlsReader::parseXls( const string &filePath ){
		m_SheetArray.clear();

		xlsWorkBook *p_Wb;
		xlsWorkSheet *p_Ws;

		p_Wb = xls_open( filePath.c_str(), "UTF-8" );
		if(  p_Wb == NULL ){
			DEBUG_I("parse failed [ " << filePath << "] ");
		}

		for(  int indexOfSheet = 0; indexOfSheet < p_Wb->sheets.count; ++indexOfSheet ){
			p_Ws = xls_getWorkSheet( p_Wb, indexOfSheet );
			xls_parseWorkSheet( p_Ws );

			m_SheetArray.push_back(  XlsSheet(*p_Ws) );

			xls_close_WS( p_Ws );  // 源码示例中没有进行delete,只有close函数。
		}

		xls_close_WB( p_Wb);  // 源码示例中没有进行delete,只有close函数。
	}
コード例 #3
0
ファイル: xls2csv.c プロジェクト: joneasterbrook/jot
int main(int argc, char *argv[]) {
    xlsWorkBook* pWB;
    xlsWorkSheet* pWS;
    unsigned int i;
    int justList = 0;
    char *sheetName = "";

    if(argc < 2) {
        Usage(argv[0]);
    }

    optind = 2; // skip file arg

    int ch;
    while ((ch = getopt(argc, argv, "lt:e:q:f:")) != -1) {
        switch (ch) {
        case 'l':
            justList = 1;
            break;
        case 'e':
            encoding = strdup(optarg);
            break;
        case 't':
            sheetName = strdup(optarg);
            break;
        case 'q':
            stringSeparator = optarg[0];
            break;
        case 'f':
            fieldSeparator = strdup(optarg);
            break;
        default:
            Usage(argv[0]);
            break;
        }
     }

    struct st_row_data* row;
    WORD cellRow, cellCol;

    // open workbook, choose standard conversion
    pWB = xls_open(argv[1], encoding);
    if (!pWB) {
        fprintf(stderr, "File not found");
        fprintf(stderr, "\n");
        return EXIT_FAILURE;
    }

    // check if the requested sheet (if any) exists
    if (sheetName[0]) {
        for (i = 0; i < pWB->sheets.count; i++) {
            if (strcmp(sheetName, pWB->sheets.sheet[i].name) == 0) {
                break;
            }
        }

        if (i == pWB->sheets.count) {
            fprintf(stderr, "Sheet \"%s\" not found", sheetName);
            fprintf(stderr, "\n");
            return EXIT_FAILURE;
        }
    }

    // process all sheets
    for (i = 0; i < pWB->sheets.count; i++) {
        int isFirstLine = 1;

        // just looking for sheet names
        if (justList) {
            printf("%s\n", pWB->sheets.sheet[i].name);
            continue;
        }

        // check if this the sheet we want
        if (sheetName[0]) {
            if (strcmp(sheetName, pWB->sheets.sheet[i].name) != 0) {
                continue;
            }
        }

        // open and parse the sheet
        pWS = xls_getWorkSheet(pWB, i);
        xls_parseWorkSheet(pWS);

        // process all rows of the sheet
        for (cellRow = 0; cellRow <= pWS->rows.lastrow; cellRow++) {
            int isFirstCol = 1;
            row = xls_row(pWS, cellRow);

            // process cells
            if (!isFirstLine) {
                printf("%s", lineSeparator);
            } else {
                isFirstLine = 0;
            }

            for (cellCol = 0; cellCol <= pWS->rows.lastcol; cellCol++) {
                //printf("Processing row=%d col=%d\n", cellRow+1, cellCol+1);

                xlsCell *cell = xls_cell(pWS, cellRow, cellCol);

                if ((!cell) || (cell->isHidden)) {
                    continue;
                }

                if (!isFirstCol) {
                    printf("%s", fieldSeparator);
                } else {
                    isFirstCol = 0;
                }

                // display the colspan as only one cell, but reject rowspans (they can't be converted to CSV)
                if (cell->rowspan > 1) {
                    fprintf(stderr, "Warning: %d rows spanned at col=%d row=%d: output will not match the Excel file.\n", cell->rowspan, cellCol+1, cellRow+1);
                }

                // display the value of the cell (either numeric or string)
                if (cell->id == 0x27e || cell->id == 0x0BD || cell->id == 0x203) {
                    OutputNumber(cell->d);
                } else if (cell->id == 0x06) {
                    // formula
                    if (cell->l == 0) // its a number
                    {
                        OutputNumber(cell->d);
                    } else {
                        if (!strcmp(cell->str, "bool")) // its boolean, and test cell->d
                        {
                            OutputString((int) cell->d ? "true" : "false");
                        } else if (!strcmp(cell->str, "error")) // formula is in error
                        {
                            OutputString("*error*");
                        } else // ... cell->str is valid as the result of a string formula.
                        {
                            OutputString(cell->str);
                        }
                    }
                } else if (cell->str != NULL) {
                    OutputString(cell->str);
                } else {
                    OutputString("");
                }
            }
        }
        xls_close_WS(pWS);
    }

    xls_close(pWB);
    return EXIT_SUCCESS;
}