C#实现导入CSV文件到Excel工作簿的方法
2015-06-29 11:33
1346 查看
本文实例讲述了C#实现导入CSV文件到Excel工作簿的方法。分享给大家供大家参考。具体如下:
你必须在项目中添加对 Microsoft.Office.Core 的引用:from the .NET tab of the Visual Studio Add Reference dialog box, and the Microsoft Excel 12.0 Object Library (you can use 14.0 if you want, too, but nothing lower).
C#代码如下:
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Core; /// <summary> /// Takes a CSV file and sucks it into the specified worksheet of this workbook at the specified range /// </summary> /// <param name="importFileName">Specifies the full path to the .CSV file to import</param> /// <param name="destinationSheet">Excel.Worksheet object corresponding to the destination worksheet.</param> /// <param name="destinationRange">Excel.Range object specifying the destination cell(s)</param> /// <param name="columnDataTypes">Column data type specifier array. For the QueryTable.TextFileColumnDataTypes property.</param> /// <param name="autoFitColumns">Specifies whether to do an AutoFit on all imported columns.</param> public void ImportCSV(string importFileName, Excel.Worksheet destinationSheet, Excel.Range destinationRange, int[] columnDataTypes, bool autoFitColumns) { destinationSheet.QueryTables.Add( "TEXT;" + Path.GetFullPath(importFileName), destinationRange, Type.Missing); destinationSheet.QueryTables[1].Name = Path.GetFileNameWithoutExtension(importFileName); destinationSheet.QueryTables[1].FieldNames = true; destinationSheet.QueryTables[1].RowNumbers = false; destinationSheet.QueryTables[1].FillAdjacentFormulas = false; destinationSheet.QueryTables[1].PreserveFormatting = true; destinationSheet.QueryTables[1].RefreshOnFileOpen = false; destinationSheet.QueryTables[1].RefreshStyle = XlCellInsertionMode.xlInsertDeleteCells; destinationSheet.QueryTables[1].SavePassword = false; destinationSheet.QueryTables[1].SaveData = true; destinationSheet.QueryTables[1].AdjustColumnWidth = true; destinationSheet.QueryTables[1].RefreshPeriod = 0; destinationSheet.QueryTables[1].TextFilePromptOnRefresh = false; destinationSheet.QueryTables[1].TextFilePlatform = 437; destinationSheet.QueryTables[1].TextFileStartRow = 1; destinationSheet.QueryTables[1].TextFileParseType = XlTextParsingType.xlDelimited; destinationSheet.QueryTables[1].TextFileTextQualifier = XlTextQualifier.xlTextQualifierDoubleQuote; destinationSheet.QueryTables[1].TextFileConsecutiveDelimiter = false; destinationSheet.QueryTables[1].TextFileTabDelimiter = false; destinationSheet.QueryTables[1].TextFileSemicolonDelimiter = false; destinationSheet.QueryTables[1].TextFileCommaDelimiter = true; destinationSheet.QueryTables[1].TextFileSpaceDelimiter = false; destinationSheet.QueryTables[1].TextFileColumnDataTypes = columnDataTypes; Logger.GetInstance().WriteLog("Importing data..."); destinationSheet.QueryTables[1].Refresh(false); if (autoFitColumns==true) destinationSheet.QueryTables[1].Destination.EntireColumn.AutoFit(); // cleanup this.ActiveSheet.QueryTables[1].Delete(); }
使用方法如下:
myOwnWorkbookClass.ImportCSV( @"C:\MyStuff\MyFile.CSV", (Excel.Worksheet)(MyWorkbook.Worksheets[1]), (Excel.Range)(((Excel.Worksheet)MyWorkbook.Worksheets[1]).get_Range("$A$7")), new int[] { 2, 2, 2, 2, 2 }, true);
希望本文所述对大家的C#程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- mysql load data 导出、导入 csv
- 使用Python生成Excel格式的图片
- c#调用COM组件
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- excel vba 限制工作表的滚动区域代码
- excel vba 高亮显示当前行代码
- C#实现把指定数据写入串口
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#托管堆对象实例包含内容分析
- C#实现按照指定长度在数字前补0方法小结