0

I would like to ask how I can create a new Excel file. I am writing a program and there is one part I can't find out how to do it.

I have a button which should create the new Excel file, so you click on it and it will open the file dialog and then save it somewhere. But I can't find out how to do it, I have tried hundreds of videos and pages but I just can't figure it out.

Could you just show me please how it is done? Thank you

2 Answers 2

3

You can try to use Microsoft.Office.Interop.Excel. Before using, please make sure you have Excel installed.

The following are the steps how to use it.

Fisrt, install the package Microsoft.Office.Interop.Excel from Nuget.

enter image description here

Then, refer to the demo code below.

private void button1_Click(object sender, EventArgs e)
{
    object Nothing = System.Reflection.Missing.Value;
    var app = new Microsoft.Office.Interop.Excel.Application();
    app.Visible = false;
    Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Add(Nothing);
    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Sheets[1];
    worksheet.Name = "WorkSheet";
    // Write data
    worksheet.Cells[1, 1] = "FileName";
    worksheet.Cells[1, 2] = "FindString";
    worksheet.Cells[1, 3] = "ReplaceString";

    // Show save file dialog
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    if (saveFileDialog.ShowDialog() == DialogResult.OK)
    {
        worksheet.SaveAs(saveFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
        workBook.Close(false, Type.Missing, Type.Missing);
        app.Quit();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is how I save excel, but the saved excel file does not open
0

You can use Spire.XLS library to create Excel in windows forms application.

First, search and install Spire.XLS for .NET library via NuGet.

Second, refer to the below example code to create Excel with it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Xls;

namespace CreateExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Range["A3"].Text = "Hello World";
            workbook.SaveToFile("Sample.xlsx", ExcelVersion.Version2013);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.