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

作者:红薯 时间:2022-09-13 12:24:02 

本文实例讲述了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#,CSV,Excel
0
投稿

猜你喜欢

  • 六款值得推荐的android(安卓)开源框架简介

    2023-06-24 01:46:54
  • 浅谈JAVA 内存流的实现

    2021-06-28 05:43:59
  • SpringBoot实战之高效使用枚举参数(原理篇)案例详解

    2022-02-10 23:54:23
  • scala 读取txt文件的方法示例

    2022-09-30 19:21:10
  • Unity3D中脚本的执行顺序和编译顺序

    2023-11-07 01:50:31
  • JAVA操作MongoDB数据库实例教程

    2023-11-18 13:22:27
  • C# 进行图片压缩的示例代码(对jpg压缩效果最好)

    2023-05-11 16:34:19
  • MyEclipse2018中安装Mybatis generator插件的实现步骤

    2022-02-17 03:47:37
  • Java文件操作工具类fileUtil实例【文件增删改,复制等】

    2023-11-28 08:39:00
  • Android三种菜单实例分析

    2023-09-09 05:17:20
  • Java检查非空的三种方法总结

    2023-10-03 19:58:16
  • java多线程之停止线程的方法实例代码详解

    2023-03-23 04:35:21
  • Java的Spring框架中bean的继承与内部bean的注入

    2023-06-17 18:50:44
  • 浅谈Java内存区域划分和内存分配策略

    2023-08-11 18:52:49
  • Unity利用材质自发光实现物体闪烁

    2021-07-03 20:42:26
  • 如何将IDEA打成jar包并在windows后台运行

    2022-02-27 01:36:57
  • Android自定义水波纹底部导航的实现

    2022-08-23 13:12:35
  • Java多线程 两阶段终止模式Two-Phase Termination Patter

    2023-11-29 04:47:04
  • C#中的Socket编程详解

    2021-10-24 01:25:00
  • Java超详细讲解设计模式中的命令模式

    2023-07-26 15:23:11
  • asp之家 软件编程 m.aspxhome.com