converting an Excel (xls) file to a comma separated (csv) file without the GUI

前端 未结 13 1700
走了就别回头了
走了就别回头了 2020-12-01 05:29

Is there a simple way to translate an XLS to a CSV formatted file without starting the Excel windowed application?

I need to process some Excel XLS workbooks with sc

相关标签:
13条回答
  • 2020-12-01 06:23

    my solution:

    use Spreadsheet::BasicRead;
    
    my $xls = 'file.xls';   
    my $csv = 'file.csv';
    
       my $ss = new Spreadsheet::BasicRead($xls) or die "Could not open '$xls': $!";
       my $name = '';
       my $row = 0;
    
       open(FILE, ">$csv") or die "Could not open : $!\n";
          flock(FILE, 2) or die "Could not lock file\n"; 
    
            while (my $data = $ss->getNextRow()){
                $row++;
                $name = join(';',@$data);         
                print FILE $name."\n" if ($name ne "");
            }
    
          flock(FILE, 8); 
       close FILE; 
    
    0 讨论(0)
  • 2020-12-01 06:23

    You can do it with Alacon - command-line utility for Alasql database.

    It works with Node.js, so you need to install Node.js and then Alasql package:

    > npm install alasql
    

    To convert Excel file to CVS (ot TSV) you can enter:

    > node alacon "SELECT * INTO CSV('mydata.csv', {headers:true}) FROM XLS('mydata.xls', {headers:true})"
    

    Alacon supports other type of conversions (CSV, TSV, TXT, XLSX, XLS) and SQL language constructions (see User Manual for examples).

    0 讨论(0)
  • 2020-12-01 06:24

    You can use xls2csv from the catdoc package if you're on Debian/Ubuntu

    0 讨论(0)
  • 2020-12-01 06:24

    In Java world you can use apache poi. You could start from the following Groovy snippet.

    FileInputStream fis = new FileInputStream(filename);
    Workbook wb = new HSSFWorkbook(fis); 
    Sheet sheet = wb.getSheetAt(0);
    for (Row row : sheet) {
      for (Cell cell : row) {
        doSomething(cell.toString())
      }
    
    }
    
    0 讨论(0)
  • 2020-12-01 06:24

    Excel can be used as datasource and there are drivers available to access EXCEL as database.

    1.) Create and Open a connection to EXCEL file, which you want to convert into CSV.

    2.) Fire a query like "SELECT * From Sheet1", which will load all the data of Sheet1 into recordset or datatable.

    3.) Since I'm using .net, I can hold those records on datatable and convert into CSV using following extension method.

            public static string ToCSV(this DataTable _dataTable)
            {
                StringBuilder csv = new StringBuilder();
                StringWriter sw = new StringWriter(csv);
                int icolcount = _dataTable.Columns.Count;
                for (int i = 0; i < icolcount; i++)
                {
                    sw.Write(_dataTable.Columns[i]);
                    if (i < icolcount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
                foreach (DataRow drow in _dataTable.Rows)
                {
                    for (int i = 0; i < icolcount; i++)
                    {
                        if (!Convert.IsDBNull(drow[i]))
                        {
                            sw.Write(drow[i].ToString());
                        }
                        if (i < icolcount - 1)
                        {
                            sw.Write(",");
                        }
                    }
                    sw.Write(sw.NewLine);
                }
                sw.Close();
                return csv.ToString();
            }
    

    You can apply this approach on the platform you're working on.

    Thanks.

    0 讨论(0)
  • 2020-12-01 06:24

    If You have Cygwin, most probablly You will have Python. If not - install python and use this script. It is much more than You need, but will convert easilly and fast.

    0 讨论(0)
提交回复
热议问题