Excel转CSV无处理过程

引用com中的Microsoft.Excel 11.0或12.0 Object Library.
将Microsoft.Office.Interop.Excel属性“嵌入互操作类型”设置为true.
 1 /// <summary>
 2         /// 将excel文件另存csv文件
 3         /// </summary>
 4         /// <param name="excelFileName">Excel文件路径</param>
 5         /// <param name="csvFileName">csv文件路径</param>
 6         public static void SaveAsExcel(string excelFileName, string csvFileName)
 7         {
 8             //定义一个COM中空类型的对象(作用类似于大家所熟悉的null)
 9             object missing = System.Reflection.Missing.Value;
10 
11             //创建Excel应用程序对象(会帮我们在启动Excel进程)
12             Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
13 
14             Microsoft.Office.Interop.Excel.Workbook wb = app.Application.Workbooks.Open(excelFileName, missing, missing, missing, missing, missing, missing, missing,
15             missing, missing, missing, missing, missing, missing, missing);
16             //不出现提示,用默认的选择
17             app.Application.DisplayAlerts = false;
18             //不运行excel界面
19             app.Application.Visible = false;
20 
21             Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
22             try
23             {
24                 //另存为csv格工,注意Excel.XlFileFormat.xlCSV参数,要另存为别的格式,也是在这里设置
25                 sheet.SaveAs(csvFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV, missing, missing, false, false, false, missing, missing, false);
26             }
27             finally
28             {
29 
30                 wb.Close(false, missing, missing);
31 
32                 app.Quit();
33             }
34         }
35         /// <summary>
36         /// 将csv文件中的数据转成dataTabel
37         /// </summary>
38         /// <param name="path">csv路径</param>
39         /// <returns>DataTable</returns>
40         public static DataTable CSVToDS(string path)
41         {
42             String line;
43             String[] split = null;
44             DataTable table = new DataTable("auto");
45             DataRow row = null;
46             StreamReader sr = new StreamReader(path, Encoding.Default);
47 
48             //创建与数据源对应的数据列
49             line = sr.ReadLine();
50             split = line.Split(',');
51             foreach (String colname in split)
52             {
53                 table.Columns.Add(colname, System.Type.GetType("System.String"));
54             }
55             //将数据填入数据表
56             int j = 0;
57             while ((line = sr.ReadLine()) != null)
58             {
59                 j = 0;
60                 row = table.NewRow();
61                 split = line.Split(',');
62                 foreach (String colname in split)
63                 {
64                     row[j] = colname;
65                     j++;
66                 }
67                 table.Rows.Add(row);
68             }
69             sr.Close();
70             //显示数据
71             return table;
72         }

 

posted @ 2014-08-13 18:59  記號  阅读(407)  评论(0编辑  收藏  举报