2014-05-12 178 views
-2

我试图制作一些脚本,当它被调用时将自动将csv文件转换为xlsx文件。我正在尝试python脚本。我没有什么问题,分隔符,因为我需要放两个delimters:选项卡和逗号将csv文件转换为xlsx

import os 
import glob 
import csv 
from xlsxwriter.workbook import Workbook 

files = ['test3.csv'] 

for i in files: 
    workbook = Workbook(i + '.xlsx') 
    worksheet = workbook.add_worksheet() 
with open('test3.csv') as f: 
     reader=csv.reader((f), delimiter=",") 
     for r, row in enumerate(reader): 
      for c, col in enumerate(row): 
       worksheet.write(r, c, col) 
workbook.close() 

此代码是好的,但我需要把里面分隔符也条件“\ t”的? 有谁知道如何?

+0

那你在Python尝试,为什么不工作?同样,你的PHP脚本不起作用? – wnnmaw

+0

看看[xlswriter标签](https://stackoverflow.com/questions/tagged/xlsxwriter)。 –

+0

Exel将打开CSV文件,是您的初始CSV格式错误(听起来像它可能不是CSV),并且您试图将其转换为制表符分隔? –

回答

0

您可以使用this,也许它可以帮助

<?php 
/** 
* CSVToExcelConverter 
*/ 
class CSVToExcelConverter 
{ 
    /** 
    * Read given csv file and write all rows to given xls file 
    * 
    * @param string $csv_file Resource path of the csv file 
    * @param string $xls_file Resource path of the excel file 
    * @param string $csv_enc Encoding of the csv file, use utf8 if null 
    * @throws Exception 
    */ 
    public static function convert($csv_file, $xls_file, $csv_enc=null) { 
     //set cache 
     $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp; 
     PHPExcel_Settings::setCacheStorageMethod($cacheMethod); 

     //open csv file 
     $objReader = new PHPExcel_Reader_CSV(); 
     if ($csv_enc != null) 
      $objReader->setInputEncoding($csv_enc); 
     $objPHPExcel = $objReader->load($csv_file); 
     $in_sheet = $objPHPExcel->getActiveSheet(); 

     //open excel file 
     $objPHPExcel = new PHPExcel(); 
     $out_sheet = $objPHPExcel->getActiveSheet(); 

     //row index start from 1 
     $row_index = 0; 
     foreach ($in_sheet->getRowIterator() as $row) { 
      $row_index++; 
      $cellIterator = $row->getCellIterator(); 
      $cellIterator->setIterateOnlyExistingCells(false); 

      //column index start from 0 
      $column_index = -1; 
      foreach ($cellIterator as $cell) { 
       // put up your delimiter or cell formatting here 
       $column_index++; 
       $out_sheet->setCellValueByColumnAndRow($column_index, $row_index, $cell->getValue()); 
      } 
     } 

     //write excel file 
     $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
     $objWriter->save($xls_file); 
    } 
} 

,这里是你如何使用它:

<?php 
if (!isset($_FILES["file"])) 
{ 
?> 
<html> 
    <body> 
     <h1>Convert CSV to XLSX</h1> 
     <form action="test.php" method="post" enctype="multipart/form-data"> 
      <input type="file" name="file"/> 
      <input type="submit"/> 
     </form> 
    </body> 
</html> 
<?php 
    exit; 
} 

//obtain PHPExcel from http://phpexcel.codeplex.com 
require_once('PHPExcel.php'); 
require_once('CSVToExcelConverter.php'); 

if ($_FILES["file"]["error"] > 0) 
{ 
    echo "Error: " . $_FILES["file"]["error"]; 
    exit; 
} 

try 
{ 
    header('Content-type: application/ms-excel'); 
    header('Content-Disposition: attachment; filename='.'test.xlsx'); 

    CSVToExcelConverter::convert($_FILES['file']['tmp_name'], 'php://output'); 
} catch(Exception $e) { 
    echo $e->getMessage(); 
} 
+0

我看到了这个解决方案,但他会按我的意愿格式化我的csv吗?我可以在哪里放置我想要的分隔符? – marija

+0

查看代码'$ column_index = -1; foreach($ cellIterator as $ cell){$ column_index ++; $ out_sheet-> setCellValueByColumnAndRow($ column_index,$ row_index,$ cell-> getValue()); }'在哪里你可以把一个分隔符 – Guns

+0

好的,谢谢,我会尝试:) – marija

3

作者是Excel2007XLSX,所以这是你传递的价值创建Writer来告诉它你想使用哪个Writer;和OfficeOpenXML格式文件没有分隔符或外壳,所以有在Excel2007中设置作家没有这些作家方法

require_once 'PHPExcel/PHPExcel/IOFactory.php'; 
$csv = PHPExcel_IOFactory::load("test.csv"); 
$writer= PHPExcel_IOFactory::createWriter($csv, 'Excel2007'); 
$writer->save("test.xlsx"); 
+0

谢谢,我会试试这:) – marija