summaryrefslogtreecommitdiff
path: root/Tools/XlsxToCsv/ExcelConvert.cs
blob: 129bb66c64e954bb819688c34f3d203b30ab9882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Excel;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;

namespace xlsxToCsv
{
    public class ExcelConvert
    {
        private DataSet result = new DataSet();

        public bool Convert(string src, string tar)
        {
            GetExcelData(src);
            return ConverToCSV(tar);
        }

        private void GetExcelData(string file)
        {
            if (file.EndsWith(".xlsx"))
            {
                // Reading from a binary Excel file (format; *.xlsx)
                FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                result = excelReader.AsDataSet();
                excelReader.Close();
            }

            if (file.EndsWith(".xls"))
            {
                // Reading from a binary Excel file ('97-2003 format; *.xls)
                FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
                IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                result = excelReader.AsDataSet();
                excelReader.Close();
            }

            List<string> items = new List<string>();
            for (int i = 0; i < result.Tables.Count; i++)
                items.Add(result.Tables[i].TableName.ToString());
        }

        private bool ConverToCSV(string toFilePath)
        {
            int index = 0;
            // sheets in excel file becomes tables in dataset
            // result.Tables[0].TableName.ToString(); // to get sheet name (table name)

            string a = "";
            int row_no = 0;
            if (result.Tables.Count == 0)
            {
                return false;
            }
            while (row_no < result.Tables[index].Rows.Count)
            {
                for (int i = 0; i < result.Tables[index].Columns.Count; i++)
                {
                    if (i - 1 < result.Tables[index].Columns.Count)
                    {
                        a += result.Tables[index].Rows[row_no][i].ToString() + ",";
                    } else
                    {
                        a += result.Tables[index].Rows[row_no][i].ToString();
                    }
                }
                row_no++;
                a = Regex.Replace(a, ",*$", "");
                a += "\n";
            }
            string output = toFilePath;
            StreamWriter csv = new StreamWriter(@output, false);
            csv.Write(a);
            csv.Close();

            return true;
        }
    }
}