c#里excel转换成csv的万能方法
最近搞了半天,都搞不定excel转换问题,总算找到一个ExcelWrapper.dll,直接在机子里面打开excel,然后读取cell,读好后把cell内容整理好,转换成csv格式。好了废话少说,下面的是我写的转换类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class xls2csv
2 {
3 //时间:2012年3月31日14:33:20
4
5
6 public xls2csv()
7 {
8
9 }
10
11 /// <summary>This method convery excel sheet to
12 /// csvfiles.</summary>
13 /// <param name="start">摘取下来的表格前面若有几行需要跳过的,则填写,默认写0</param>
14 /// <param name="toend">同上,后面有几行不要就填几,默认写0</param>
15 public static void convert(string xlsPath, string csvPath,int start,int toend)
16 {
17 ExcelWrapper.Wrapper wapper = new ExcelWrapper.Wrapper();
18 wapper.Open(xlsPath, true);
19 char col = 'A';
20 int row = wapper.WorksheetCount;
21 string name = wapper.WorksheetNames[0];
22 int row_count=0;
23 int col_count;
24 int count = 1;
25
26 //有多少行 1,2,3,4,5,6.。。。
27 try
28 {
29 while (!string.IsNullOrEmpty(wapper.GetCellValue(col + count.ToString())))
30 {
31 count++;
32 }
33 }
34 catch (NullReferenceException e)
35 {
36 row_count = count-1;
37 }
38 row_count = count-1;
39
40 StreamWriter fileWriter = new StreamWriter(csvPath, false, Encoding.Default);
41 //如果没有行 则跳出
42 if (row_count < 1)
43 { }
44 else
45 {
46
47 //有多少列 A,B,C,D,E,.....
48 try
49 {
50 string tmp = (row_count > 1) ? ((row_count / 2 + 1).ToString()) : ("1");
51 while (!string.IsNullOrEmpty(wapper.GetCellValue(col + tmp)))
52 {
53 col++;
54 }
55 }
56 catch (NullReferenceException e)
57 {
58 col_count = col - 1;
59 }
60 col_count = col - 1;
61
62
63
64 for (int i = 1 + start; i <= row_count - toend; i++)//行
65 {
66 string line = "";
67 string pos;
68 for (char a = 'A'; a <= col_count; a++)//列
69 {
70 try
71 {
72 pos = a + i.ToString();
73 line += wapper.GetCellValue(pos).Trim();
74 line += ",";
75
76 }
77 catch (NullReferenceException e)
78 {
79 line += ",";
80 Console.WriteLine("position:{1}{0} is empty", i, a);
81 }
82 }
83 fileWriter.WriteLine(line);
84 // Console.WriteLine(line);
85 }
86
87 }
88 fileWriter.Flush();
89 fileWriter.Close();
90 wapper.Close();
91
92 // Console.ReadLine();
93 }
94 }
关于ExcelWrapper.dll,好像不好发。这个有点就是兼容性好,虽然执行效率不高,但现在计算机速度也很快。
上次用的代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 static void convertExcelToCSV(string sourceFile, string worksheetName, string targetFile)
2 {
3 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourceFile +
4 ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
5
6 OleDbConnection conn = null;
7 StreamWriter wrtr = null;
8 OleDbCommand cmd = null;
9 OleDbDataAdapter da = null;
10
11 try
12 {
13 conn = new OleDbConnection(strConn);
14 conn.Open();
15
16 //cmd = new OleDbCommand("SELECT * FROM " + worksheetName, conn);
17 cmd = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]", conn);
18 cmd.CommandType = CommandType.Text;
19 wrtr = new StreamWriter(targetFile);
20
21 da = new OleDbDataAdapter(cmd);
22 DataTable dt = new DataTable();
23 da.Fill(dt);
24
25 for (int x = 0; x < dt.Rows.Count; x++)
26 {
27 string rowString = "";
28 for (int y = 0; y < dt.Columns.Count; y++)
29 {
30 rowString += "\"" + dt.Rows[x][y].ToString() + "\",";
31 }
32 wrtr.WriteLine(rowString);
33 }
34 Console.WriteLine();
35 Console.WriteLine("Done! Your " + sourceFile + " has been converted into " + targetFile+".");
36 Console.WriteLine();
37 }
38 catch (Exception exc)
39 {
40 Console.WriteLine(exc.ToString());
41 Console.ReadLine();
42 }
43 finally
44 {
45 if (conn.State == ConnectionState.Open)
46 conn.Close();
47 conn.Dispose();
48 cmd.Dispose();
49 da.Dispose();
50 wrtr.Close();
51 wrtr.Dispose();
52 }
53 }
主要不兼容64位模式,折腾了好久,这次总算解决了!
文件这样上传,不知道对不对
https://files.cnblogs.com/grey/ExcelWrapper.zip