Csharp/C#教程:C#实现导入CSV文件到Excel工作簿的方法分享

本文实例讲述了C#实现导入CSV文件到Excel工作簿的方法。分享给大家供大家参考。具体如下:

你必须在项目中添加对Microsoft.Office.Core的引用:fromthe.NETtaboftheVisualStudioAddReferencedialogbox,andtheMicrosoftExcel12.0ObjectLibrary(youcanuse14.0ifyouwant,too,butnothinglower).

C#代码如下:

usingMicrosoft.Office.Interop.Excel; usingMicrosoft.Office.Core; ///<summary> ///TakesaCSVfileandsucksitintothespecifiedworksheetofthisworkbookatthespecifiedrange ///</summary> ///<paramname="importFileName">Specifiesthefullpathtothe.CSVfiletoimport</param> ///<paramname="destinationSheet">Excel.Worksheetobjectcorrespondingtothedestinationworksheet.</param> ///<paramname="destinationRange">Excel.Rangeobjectspecifyingthedestinationcell(s)</param> ///<paramname="columnDataTypes">Columndatatypespecifierarray.FortheQueryTable.TextFileColumnDataTypesproperty.</param> ///<paramname="autoFitColumns">SpecifieswhethertodoanAutoFitonallimportedcolumns.</param> publicvoidImportCSV(stringimportFileName,Excel.WorksheetdestinationSheet, Excel.RangedestinationRange,int[]columnDataTypes,boolautoFitColumns) { 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("Importingdata..."); destinationSheet.QueryTables[1].Refresh(false); if(autoFitColumns==true) destinationSheet.QueryTables[1].Destination.EntireColumn.AutoFit(); //cleanup this.ActiveSheet.QueryTables[1].Delete(); }

使用方法如下:

myOwnWorkbookClass.ImportCSV( @"C:MyStuffMyFile.CSV", (Excel.Worksheet)(MyWorkbook.Worksheets[1]), (Excel.Range)(((Excel.Worksheet)MyWorkbook.Worksheets[1]).get_Range("$A$7")), newint[]{2,2,2,2,2},true);

希望本文所述对大家的C#程序设计有所帮助。

您可能感兴趣的文章:C#中将DataTable转换成CSV文件的方法C#导出数据到CSV文件的通用类实例C#操作CSV文件类实例C#读取csv格式文件的方法C#解析Excel并且生成Csv文件代码分析C#中csv文件与DataTable互相导入处理实例解析一个读写csv文件的C#类

标签: ce 方法

C语言数据结构之循环链表的简单实例

C++ 中友元函数与友元类详解

上述就是C#学习教程:C#实现导入CSV文件到Excel工作簿的方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906619.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐