教你C#將CSV轉(zhuǎn)為Excel的實(shí)現(xiàn)方法
CSV(Comma Separated Values)文件是一種純文本文件,包含用逗號(hào)分隔的數(shù)據(jù),常用于將數(shù)據(jù)從一個(gè)應(yīng)用程序?qū)牖驅(qū)С龅搅硪粋€(gè)應(yīng)用程序。通過(guò)將CSV文件轉(zhuǎn)為EXCEL,可執(zhí)行更多關(guān)于數(shù)據(jù)編輯、格式設(shè)置等操作。下面,將通過(guò)C#及VB.NET代碼展示如何來(lái)實(shí)現(xiàn)轉(zhuǎn)換。
一、程序環(huán)境
可通過(guò)以下途徑來(lái)安裝Excel庫(kù):
1. 通過(guò)NuGet安裝Spire.XLS;
2. 官方下載包,解壓安裝到本地指定路徑。在Visual Studio中打開(kāi)“解決方案資源管理器”,將本地安裝路徑下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);
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//訪問(wèn)工作表中使用的范圍
CellRange usedRange = sheet.AllocatedRange;
//當(dāng)將范圍內(nèi)的數(shù)字保存為文本時(shí),忽略錯(cuò)誤
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)
'獲取第一個(gè)工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
'訪問(wèn)工作表中使用的范圍
Dim usedRange As CellRange = sheet.AllocatedRange
'當(dāng)將范圍內(nèi)的數(shù)字保存為文本時(shí),忽略錯(cuò)誤
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ǔ)充知識(shí):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);
/// 刪除一個(gè)指定的文件
/// <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í)行過(guò)程中可能會(huì)打開(kāi)多個(gè)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的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)C# CSV轉(zhuǎn)為Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你C#將CSV轉(zhuǎn)為Excel的實(shí)現(xiàn)方法
這篇文章主要介紹了C#?將CSV轉(zhuǎn)為Excel,轉(zhuǎn)換之后可執(zhí)行更多關(guān)于數(shù)據(jù)編輯、格式設(shè)置等操作,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-03-03
C#中的自動(dòng)類型轉(zhuǎn)換和強(qiáng)制類型轉(zhuǎn)換
這篇文章主要介紹了C#中的自動(dòng)類型轉(zhuǎn)換和強(qiáng)制類型轉(zhuǎn)換,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-08-08
Visual Studio連接unity編輯器的實(shí)現(xiàn)步驟
unity編輯器中打開(kāi)C#腳本的時(shí)候發(fā)現(xiàn)Visual Studio沒(méi)有連接unity編輯器,本文主要介紹了Visual Studio連接unity編輯器的實(shí)現(xiàn)步驟,感興趣的可以了解一下2023-11-11
C#使用Lazy<T>實(shí)現(xiàn)對(duì)客戶訂單的延遲加載
這篇文章介紹了C#使用Lazy<T>實(shí)現(xiàn)對(duì)客戶訂單延遲加載的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Silverlight文件上傳下載實(shí)現(xiàn)方法(下載保存)
這篇文章主要介紹了Silverlight文件上傳下載實(shí)現(xiàn)方法(下載保存) ,需要的朋友可以參考下2015-11-11
C#實(shí)現(xiàn)讓窗體永遠(yuǎn)在窗體最前面顯示的實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)讓窗體永遠(yuǎn)在窗體最前面顯示,功能非常實(shí)用,需要的朋友可以參考下2014-07-07

