Question Create a Excel file

Saeed58

New member
Joined
Oct 13, 2018
Messages
2
Programming Experience
Beginner
Hello guys!
I want to create Excell file with C#.
I used Excell library in my program and I could create Excell file but when I want to open the Excell file , excell says this file has some errors and I can not open it!

Please help!
Thanks
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,049
Location
Sydney, Australia
Programming Experience
10+
That would tend to suggest that you did something wrong. If you don't show us what you did then we can't possibly tell you what's wrong with it. It is also relevant whether you are trying to create an XLS or XLSX file, or maybe something else.
 

Saeed58

New member
Joined
Oct 13, 2018
Messages
2
Programming Experience
Beginner
Code

This is my code:

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExcelLibrary.SpreadSheet;
using ExcelLibrary.CompoundDocumentFormat;
using System.IO;

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

        private void button1_Click(object sender, EventArgs e)
        {
            string path = "d:ali.xls";
            Workbook workbook = new Workbook();
            Worksheet worksheet = new Worksheet("First Sheet");
            worksheet.Cells[0, 1] = new Cell((short)1);
            worksheet.Cells[2, 0] = new Cell(9999999);
            worksheet.Cells[3, 3] = new Cell((decimal)3.45);
            worksheet.Cells[2, 2] = new Cell("Text string");
            worksheet.Cells[2, 4] = new Cell("Second string");
            worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");
            worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY-MM-DD");
            worksheet.Cells.ColumnWidth[0, 1] = 3000;
            workbook.Worksheets.Add(worksheet);
            workbook.Save(path);
            MessageBox.Show("file created");
            Workbook book = Workbook.Load(path);
            Worksheet sheet = book.Worksheets[0];


        }
    }
}
 
Last edited by a moderator:

marklynch

New member
Joined
Jul 24, 2020
Messages
1
Programming Experience
Beginner
You can try ZetExcel If you need Excel generation functionality for your .net application.
 
Top Bottom