PhpSpreadsheet-获取d / m / Y日期的原始字符串值,而不是转换为浮点数

时间:2019-03-19 22:28:08

标签: php excel spreadsheet phpspreadsheet

我正在尝试读取一种格式为03/05/2008的Excel文件,但是当我使用PhpSpreadsheet进行读取时,它会返回2008.0

有没有办法获取列的原始字符串格式而不是转换为浮点数?

try {
    $inputFileType = IOFactory::identify($path);

    try {
        $reader = IOFactory::createReader($inputFileType);
        $reader->setReadDataOnly(true);
        $valuesSpreadsheet = $reader->load($path);

        try {
           $spreadsheetArr = $valuesSpreadsheet->getActiveSheet()->toArray();
           dd($spreadsheetArr);
        }
     }
}

编辑:我不想获得特定的单元格并将其转换为时间戳,如下面的注释。我想获取数组->toArray(),但获取所有原始字符串格式。

3 个答案:

答案 0 :(得分:1)

toArray()具有一个参数,用于返回在电子表格中格式化的单元格值。尝试这样称呼它:

$spreadsheetArr = $valuesSpreadsheet->getActiveSheet()->toArray(null, true, true, true);

this page的大约80%是toArray()函数的文档。

简而言之,toArray()可以接受4个参数:

  1. 您希望空单元格返回的任何值
  2. (布尔值)是否需要计算公式?
  3. (布尔值)单元格格式很重要吗?
  4. (布尔值)您要使用电子表格的行和列对数组进行索引吗?

答案 1 :(得分:0)

在加载数据之前取出$ reader-> setReadDataOnly(true)行,并且值应正确显示。如果没有,您也可以尝试以下代码。

@app.callback(Output('uploaded-data', 'hidden'),
              [Input('upload-data', 'contents')],
              [State('upload-data', 'filename')])
def update_hidden_datatable(contents, filename):
    global base_data
    if contents is not None:
        content_type, content_string = contents.split(',')
        decoded = base64.b64decode(content_string)
        try:
            if '.csv' in filename:
                # Assume that the user uploaded a CSV file
                df = pd.read_csv(io.StringIO(decoded.decode('utf-8')), low_memory = False)
            elif '.xls' in filename:
                # Assume that the user uploaded an excel file
                df = pd.read_excel(io.BytesIO(decoded))
        except Exception as e:
            print(e)
            return base_data.to_dict('r')
        for column in df.select_dtypes(include=[bool]):
            df[column] = df[column].map({True: 'true', False: 'false'})
        return df.to_dict('r')

    return base_data.to_dict('r')

# Updates the rows of the main datatable when the hidden uploaded-data value is updated
@app.callback(Output('datatable', 'rows'),
              [Input('uploaded-data', 'hidden')])
def update_datatable(hidden):
    return hidden

答案 2 :(得分:0)

您应该使用getRowIterator()getCellIterator()函数在所有单元格中循环。在下面的代码中,所有单元格都将作为原始值返回。

try {
    $inputFileType = IOFactory::identify($path);
    try {
        $reader = IOFactory::createReader($inputFileType);
        $reader->setReadDataOnly(true);
        $spreadsheet = $reader->load($path);
        $worksheet = $spreadsheet->getActiveSheet();\
        foreach ($worksheet->getRowIterator() as $index => $row) {
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(FALSE); //This loops through all cells
            $cells = [];
            foreach ($cellIterator as $cell) {
                $cells[] = $cell->getValue();
            }
            $rows[] = $cells;
            print_r($rows);
        }
    }
}