Reputation: 61
I'm trying to build a C# application that tracks clients data and payments. I'd prefer to utilize an excel sheet as a data sink and source. When the user launches the program it will check if the file exists to load it, if not it will create the file and use it as the data source/sink. I'm familiar with Excel Interop and functionalities. My question is how would I create the file when the user installs the software for the first time. Should I use it as a data source? and how could it be done?
Upvotes: 1
Views: 143
Reputation: 36310
Instead of using Excel interop you could use a dedicated .net library to do this. EpPlus is a nice alternative. This will give better performance than doing interop.
Upvotes: 1
Reputation: 10175
You can check for the excel file's existence before using (storing, querying, etc) it. If it does not exists, you have to create it. Please see the code below.
using System.IO;
namespace Portal.WebApp.Models
{
public class ExcelFileBuilder
{
public void CreateIfNotExisting(string excelPath)
{
if (!File.Exists(excelPath))
{
var app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks.Add();
wb.SaveAs(excelPath);
wb.Close();
}
}
}
}
Upvotes: 0