欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-13 08:03:28
本文实例讲述了c#实现导入csv文件到excel工作簿的方法。分享给大家供大家参考。具体如下: 你必须在项目中添加对 microsoft.office.core 的引用:...

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