教你C#將CSV轉(zhuǎn)為Excel的實現(xiàn)方法
CSV(Comma Separated Values)文件是一種純文本文件,包含用逗號分隔的數(shù)據(jù),常用于將數(shù)據(jù)從一個應(yīng)用程序?qū)牖驅(qū)С龅搅硪粋€應(yīng)用程序。通過將CSV文件轉(zhuǎn)為EXCEL,可執(zhí)行更多關(guān)于數(shù)據(jù)編輯、格式設(shè)置等操作。下面,將通過C#及VB.NET代碼展示如何來實現(xiàn)轉(zhuǎn)換。
一、程序環(huán)境
可通過以下途徑來安裝Excel庫:
1. 通過NuGet安裝Spire.XLS;
2. 官方下載包,解壓安裝到本地指定路徑。在Visual Studio中打開“解決方案資源管理器”,將本地安裝路徑下Bin文件夾下的dll添加引用至程序。

二、將CSV轉(zhuǎn)為Excel
C#
using Spire.Xls;
namespace CSVtoExcel_XLS
{
class Program
{
static void Main(string[] args)
{
//加載CSV文件
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.csv", ",", 1, 1);
//獲取第一個工作表
Worksheet sheet = workbook.Worksheets[0];
//訪問工作表中使用的范圍
CellRange usedRange = sheet.AllocatedRange;
//當(dāng)將范圍內(nèi)的數(shù)字保存為文本時,忽略錯誤
usedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText;
//自適應(yīng)行高、列寬
usedRange.AutoFitColumns();
usedRange.AutoFitRows();
//保存文檔
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("result.xlsx");
}
}
}VB.NET
Imports Spire.Xls
Namespace CSVtoExcel_XLS
Class Program
Private Shared Sub Main(args As String())
'加載CSV文件
Dim workbook As New Workbook()
workbook.LoadFromFile("test.csv", ",", 1, 1)
'獲取第一個工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
'訪問工作表中使用的范圍
Dim usedRange As CellRange = sheet.AllocatedRange
'當(dāng)將范圍內(nèi)的數(shù)字保存為文本時,忽略錯誤
usedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText
'自適應(yīng)行高、列寬
usedRange.AutoFitColumns()
usedRange.AutoFitRows()
'保存文檔
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2013)
System.Diagnostics.Process.Start("result.xlsx")
End Sub
End Class
End Namespace
補(bǔ)充知識:C# .csv文件轉(zhuǎn)為Excel格式;Excel格式轉(zhuǎn)換為.csv,代碼如下所示:
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Excel=Microsoft.Office.Interop.Excel;
namespace WinFromAPP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 將Csv文件轉(zhuǎn)換為XLS文件
/// </summary>
/// <param name="FilePath">文件全路路徑</param>
/// <returns>返回轉(zhuǎn)換后的Xls文件名</returns>
public static string CSVSaveasXLS(string FilePath)
QuertExcel();
string _NewFilePath = "";
Excel.Application excelApplication;
Excel.Workbooks excelWorkBooks = null;
Excel.Workbook excelWorkBook = null;
Excel.Worksheet excelWorkSheet = null;
try
{
excelApplication = new Excel.ApplicationClass();
excelWorkBooks = excelApplication.Workbooks;
excelWorkBook = ((Excel.Workbook)excelWorkBooks.Open(FilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value));
excelWorkSheet = (Excel.Worksheet)excelWorkBook.Worksheets[1];
excelApplication.Visible = false;
excelApplication.DisplayAlerts = false;
_NewFilePath = FilePath.Replace(".csv", ".xls");
excelWorkBook.SaveAs(_NewFilePath, Excel.XlFileFormat.xlAddIn, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excelWorkBook.Close();
QuertExcel();
// ExcelFormatHelper.DeleteFile(FilePath);
//可以不用殺掉進(jìn)程QuertExcel();
GC.Collect(System.GC.GetGeneration(excelWorkSheet));
GC.Collect(System.GC.GetGeneration(excelWorkBook));
GC.Collect(System.GC.GetGeneration(excelApplication));
}
catch (Exception exc)
throw new Exception(exc.Message);
finally
GC.Collect();
return _NewFilePath;
/// 將xls文件轉(zhuǎn)換為csv文件
/// <returns>返回轉(zhuǎn)換后的csv文件名</returns>
public static string XLSSavesaCSV(string FilePath)
_NewFilePath = FilePath.Replace(".xls", ".csv");
// excelWorkSheet._SaveAs(FilePath, Excel.XlFileFormat.xlCSVWindows, Missing.Value, Missing.Value, Missing.Value,Missing.Value,Missing.Value, Missing.Value, Missing.Value);
excelWorkBook.SaveAs(_NewFilePath, Excel.XlFileFormat.xlCSV, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//ExcelFormatHelper.DeleteFile(FilePath);
/// 刪除一個指定的文件
/// <param name="FilePath">文件路徑</param>
/// <returns></returns>
public static bool DeleteFile(string FilePath)
bool IsFind = File.Exists(FilePath);
if (IsFind)
{
File.Delete(FilePath);
}
else
throw new IOException("指定的文件不存在");
return true;
/// 執(zhí)行過程中可能會打開多個EXCEL文件 所以殺掉
private static void QuertExcel()
Process[] excels = Process.GetProcessesByName("EXCEL");
foreach (var item in excels)
item.Kill();
private void btnConvert_Click(object sender, EventArgs e)
//CSVSaveasXLS(textBox1.Text);
XLSSavesaCSV(textBox1.Text);
}
}到此這篇關(guān)于教你C#將CSV轉(zhuǎn)為Excel的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)C# CSV轉(zhuǎn)為Excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于C#動手實現(xiàn)網(wǎng)絡(luò)服務(wù)器Web Server
這篇文章主要為大家詳細(xì)介紹了基于C#動手實現(xiàn)網(wǎng)絡(luò)服務(wù)器Web Server,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
C#實現(xiàn)根據(jù)數(shù)字序號輸出星期幾的簡單實例
這篇文章主要介紹了C#實現(xiàn)根據(jù)數(shù)字序號輸出星期幾的簡單實例,代碼簡潔實用,也有助于初學(xué)者更好的理解C#的switch和if語句的流程控制,需要的朋友可以參考下2014-07-07
在Winform程序中使用Spire.Pdf實現(xiàn)頁面添加印章功能的實現(xiàn)
這篇文章主要介紹了在Winform程序中使用Spire.Pdf實現(xiàn)頁面添加印章功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

