欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)導(dǎo)入CSV文件到Excel工作簿的方法

 更新時間:2015年06月29日 11:33:32   作者:紅薯  
這篇文章主要介紹了C#實現(xiàn)導(dǎo)入CSV文件到Excel工作簿的方法,涉及C#針對office組件的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了C#實現(xiàn)導(dǎo)入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#程序設(shè)計有所幫助。

相關(guān)文章

  • c#中多線程間的同步示例詳解

    c#中多線程間的同步示例詳解

    使用線程時最頭痛的就是共享資源的同步問題,處理不好會得到錯誤的結(jié)果,所以下面這篇文章主要給大家介紹了關(guān)于c#中多線程間同步的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • 詳解C#打開和關(guān)閉可執(zhí)行文件

    詳解C#打開和關(guān)閉可執(zhí)行文件

    這篇文章主要介紹了C#打開和關(guān)閉可執(zhí)行文件,以QQ應(yīng)用程序為例,需要的朋友可以參考下
    2015-12-12
  • C#操作注冊表的方法

    C#操作注冊表的方法

    以下從‘讀’‘寫’‘刪除’‘判斷’四個事例實現(xiàn)對注冊表的簡單操作
    2007-03-03
  • C#科學(xué)繪圖之使用scottPlot繪制多個圖像

    C#科學(xué)繪圖之使用scottPlot繪制多個圖像

    ScottPlot是基于.Net的一款開源免費的交互式可視化庫,支持Winform和WPF等UI框架,本文主要為大家詳細(xì)介紹了如何使用scottPlot實現(xiàn)繪制多個圖像,需要的可以參考下
    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應(yīng)用程序提供二維碼編碼輸出功能??梢詫⒍S碼文件導(dǎo)出為eps格式
    2016-12-12
  • c#類的使用示例

    c#類的使用示例

    這篇文章主要介紹了c#類的使用示例,還有我學(xué)習(xí)時的筆記,需要的朋友可以參考下
    2014-04-04
  • C#交錯數(shù)組用法實例

    C#交錯數(shù)組用法實例

    這篇文章主要介紹了C#交錯數(shù)組用法,較為詳細(xì)的分析了交錯數(shù)組的概念、用法并實例分析了交錯數(shù)組的使用技巧,需要的朋友可以參考下
    2015-04-04
  • C#對list列表進(jìn)行隨機(jī)排序的方法

    C#對list列表進(jìn)行隨機(jī)排序的方法

    這篇文章主要介紹了C#對list列表進(jìn)行隨機(jī)排序的方法,涉及C#操作list列表的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04

最新評論