将xlsx导出为CSV

时间:2018-11-27 13:54:13

标签: c# excel csv xlsx xls

Estou exportando um planilha excel para csv,nela tem um a colunaobservaçãoque possui em alguns registros tem mais de 255caráctermas como o primeiro registronãotem eele esta truncado os demais a 255 mesmo

segue o codigo

static void ConvertExcelToCsv(string excelFilePath, string csvOutputFile, int worksheetNumber = 1)
    {
        // connection string
        var cnnStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=Excel 12.0;");
        var cnn = new OleDbConnection(cnnStr);

        // get schema, then data
        var dt = new DataTable();
        try
        {
            cnn.Open();
            var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            if (schemaTable.Rows.Count < worksheetNumber) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet");
            string worksheet = schemaTable.Rows[worksheetNumber - 1]["table_name"].ToString().Replace("'", "");
            string sql = String.Format("select * from [{0}]", worksheet);

            var da = new OleDbDataAdapter(sql, cnn);
            da.Fill(dt);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            cnn.Close();
        }

        int vI = 0;
        using (var wtr = new StreamWriter(csvOutputFile, true, Encoding.Default))
        {
            // Print column
            foreach (DataColumn col in dt.Columns)
            {
                vI = vI + 1;
                wtr.Write(col.ColumnName);
                if (!(dt.Columns.Count == vI))
                {
                    wtr.Write(";");
                }
            }
            wtr.WriteLine("");
            vI = 0;
            foreach (DataRow row in dt.Rows)
            {
                bool firstLine = true;
                foreach (DataColumn col in dt.Columns)
                {

                    vI = vI + 1;
                    if ((!firstLine) || (dt.Columns.Count < vI))
                    {
                        wtr.Write(";");
                    }

                    else { firstLine = false; }
                    //var data = row[col.ColumnName].ToString().Replace("\"", "\"\"");
                    if (row[col.ColumnName].ToString().Length > 254)
                    {
                        var data = row[col.ColumnName.ToString()].ToString().Replace("\"", "").Replace(";", "");
                        wtr.Write(data);
                    }
                    else
                    {
                        var data = row[col.ColumnName.ToString()].ToString().Replace("\"", "").Replace(";", "");
                        wtr.Write(data);
                    }

                    //wtr.Write(String.Format("\"{0}\"", data));
                    //wtr.Write(data);
                }
                wtr.WriteLine();
                vI = 0;

            }
        }
    }

0 个答案:

没有答案