C#实现导入CSV文件到Excel工作簿的方法

 更新时间:2015年06月29日 11:33:32   作者:红薯  
这篇文章主要介绍了C#实现导入CSV文件到Excel工作簿的方法,涉及C#针对office组件的相关操作技巧,需要的朋友可以参考下

本文实例讲述了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#程序设计有所帮助。

相关文章

  • C#、.Net中把字符串(String)格式转换为DateTime类型的三种方法

    C#、.Net中把字符串(String)格式转换为DateTime类型的三种方法

    这篇文章主要介绍了C#、.Net中把字符串(String)格式转换为DateTime类型的三种方法,本文总结了Convert.ToDateTime(string)、Convert.ToDateTime(string, IFormatProvider)、DateTime.ParseExact()三种方法,需要的朋友可以参考下
    2015-07-07
  • C#使用Parallel类进行多线程编程实例

    C#使用Parallel类进行多线程编程实例

    这篇文章主要介绍了C#使用Parallel类进行多线程编程的方法,实例分析了Parallel类的相关使用技巧,需要的朋友可以参考下
    2015-06-06
  • C#中按指定质量保存图片的实例代码

    C#中按指定质量保存图片的实例代码

    这篇文章主要介绍了C#中按指定质量保存图片的实例代码,有需要的朋友可以参考一下
    2013-12-12
  • C# File类中的文件读写方法详解

    C# File类中的文件读写方法详解

    C#提供了多种操作文件的方案,尤其是File类中封装的静态方法,本文将通过一些简单的示例为大家讲讲C#读写文件的方法,需要的可以参考一下
    2023-05-05
  • c#自定义Attribute获取接口实现示例代码

    c#自定义Attribute获取接口实现示例代码

    这篇文章主要给大家介绍了关于c#自定义Attribute获取接口实现的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • 基于C#实现员工IC卡的读写功能

    基于C#实现员工IC卡的读写功能

    这篇文章主要为大家详细介绍了C#如何实现读写员工IC卡的功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2023-01-01
  • C#实现梳排序的使用示例

    C#实现梳排序的使用示例

    梳排序算法是一种改进的冒泡排序算法,它通过调整冒泡排序的间隔来提高排序的效率,本文主要介绍了C#实现梳排序的使用示例,感兴趣的可以了解一下
    2023-11-11
  • C#单例类的实现方法

    C#单例类的实现方法

    这篇文章主要介绍了C#单例类的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • C# 中如何利用lambda实现委托事件的挂接

    C# 中如何利用lambda实现委托事件的挂接

    在写一个小程序的时候,碰到了这样的问题,需要用委托来挂接事件,但是又想在这事件中使用局部的变量,而委托一旦定义好后,挂接方就没有办法再添加额外的形参了。那有没有什么办法,可以实现呢
    2013-07-07
  • 一文带你了解WPF中的附加事件

    一文带你了解WPF中的附加事件

    附加事件可用于在非元素类中定义新的 路由事件 ,并在树中的任何元素上引发该事件。本文通过简单的示例为大家讲解一下WPF附加事件的用法,需要的可以参考一下
    2022-12-12

最新评论