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#中多线程间的同步示例详解

    c#中多线程间的同步示例详解

    使用线程时最头痛的就是共享资源的同步问题,处理不好会得到错误的结果,所以下面这篇文章主要给大家介绍了关于c#中多线程间同步的相关资料,需要的朋友可以参考下
    2021-09-09
  • 详解C#打开和关闭可执行文件

    详解C#打开和关闭可执行文件

    这篇文章主要介绍了C#打开和关闭可执行文件,以QQ应用程序为例,需要的朋友可以参考下
    2015-12-12
  • C#操作注册表的方法

    C#操作注册表的方法

    以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作
    2007-03-03
  • C#科学绘图之使用scottPlot绘制多个图像

    C#科学绘图之使用scottPlot绘制多个图像

    ScottPlot是基于.Net的一款开源免费的交互式可视化库,支持Winform和WPF等UI框架,本文主要为大家详细介绍了如何使用scottPlot实现绘制多个图像,需要的可以参考下
    2023-12-12
  • C#中@字符d是个什么意思

    C#中@字符d是个什么意思

    这篇文章主要介绍了C#中@字符d是个什么意思?具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • C#文件管理类Directory实例分析

    C#文件管理类Directory实例分析

    这篇文章主要介绍了C#文件管理类Directory,非常实用,需要的朋友可以参考下
    2014-08-08
  • C#利用QrCode.Net生成二维码(Qr码)的方法

    C#利用QrCode.Net生成二维码(Qr码)的方法

    QrCode.Net是一个使用C#编写的用于生成二维码图片的类库,使用它可以非常方便的为WinForm、WebForm、WPF、Silverlight和Windows Phone 7应用程序提供二维码编码输出功能。可以将二维码文件导出为eps格式
    2016-12-12
  • c#类的使用示例

    c#类的使用示例

    这篇文章主要介绍了c#类的使用示例,还有我学习时的笔记,需要的朋友可以参考下
    2014-04-04
  • C#交错数组用法实例

    C#交错数组用法实例

    这篇文章主要介绍了C#交错数组用法,较为详细的分析了交错数组的概念、用法并实例分析了交错数组的使用技巧,需要的朋友可以参考下
    2015-04-04
  • C#对list列表进行随机排序的方法

    C#对list列表进行随机排序的方法

    这篇文章主要介绍了C#对list列表进行随机排序的方法,涉及C#操作list列表的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04

最新评论