C#实现导入CSV文件到Excel工作簿的方法
作者:红薯 发布时间:2022-09-13 12:24:02
标签:C#,CSV,Excel
本文实例讲述了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#程序设计有所帮助。


猜你喜欢
- 背景项目中要实现横向列表的无限循环滚动,自然而然想到了RecyclerView,但我们常用的RecyclerView是不支持无限循环滚动的,
- 下面分享的是一个Java多线程模拟停车场系统的小实例(Java的应用还是很广泛的,哈哈),具体代码如下:Park类public class
- 本文以实例代码实现了C#根据数字序号输出星期几,用户可通过输入数字0~6,输出星期各天的英语单词,程序中主要是演示if语句和switch语句
- 业务处理这是通过 Spring 在 Controller中注入Service模型层而在 Service模型层 结合 Mybatis / My
- 本文实例为大家分享了Android扫描和生成二维码的具体代码,供大家参考,具体内容如下MainActivity.javapublic cla
- 编程中经常有用到MD5加密的情况,Java语言并没有像PHP一样提供原生的MD5加密字符串的函数,需要MD5加密的时候,往往需要自己写。代码
- Application.Idle()方法表示:当应用程序处于空闲状态时执行相应代码。示例程序1、界面设计:一个简单的Lable控件2、代码u
- 一、Application是什么?Application类在每一次开发当中是我们都会遇到的,每一个APP都会有一个Application实例
- 本文实例讲述了Android编程简单实现九宫格。分享给大家供大家参考,具体如下:实现的步骤1. 一个整体的容器部分。就是上图中包括整个图片项
- 1.写在前面在JavaFX的程序开发的时候,在使用多线程的时候,默认情况下在程序退出的时候,新开的线程依然在后台运行。 在这种情况下,可以监
- 最近上线的项目中数据库数据已经临近饱和,最大的一张表数据已经接近3000W,百万数据的表也有几张,项目要求读数据(select)时间不能超过
- Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法。被传递的对象需要先实现序列化,
- Android Notification使用详解Notification核心代码(链式调用):适用于Android 4.0以上(
- 一、Android Color设置1、在xml文件中想设置颜色直接设置background的属性或者其他的color属性。随便设置一个颜色如
- **请注意!请注意!!!**今天讲给大家讲解非常“有用”的设计模式,解释器模式!!!设计模式有三大种类,一种是创建型模式,一种是结构型模式,
- 众所周知,android里面我们很熟悉的一个功能,侧滑菜单效果在以前我们大部分都是用的slidingmenu这个开源框架,自从谷歌官方新出的
- 本文实例为大家分享了Android仿刮刮卡效果展示的具体代码,供大家参考,具体内容如下一、Xfermode 通过使用Xfermode将绘制的
- 本文实例为大家分享了java将一个目录下的所有文件复制n次的具体代码,供大家参考,具体内容如下1. 文件复制示意图 2.java程
- 本文实例讲述了Java构造代码块,静态代码块原理与用法。分享给大家供大家参考,具体如下:本文内容:局部代码块构造代码块静态代码块补充&nbs
- 1、Java字符串在 Java 中字符串被作为 String 类型的对象处理。 String 类位于 java.lang 包中,默认情况下该