Ren
Ren

Reputation: 155

c# data to excel export is not exporting

I'm currently working on some c# and have some data that I'd like to export as a Excel file.

I tried making this first test:

        private void myButton11_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application excel;
            Microsoft.Office.Interop.Excel.Workbook excelworkbook;
            Microsoft.Office.Interop.Excel.Worksheet excelsheet;

            excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Visible = false;
            excel.DisplayAlerts = false;

            excelworkbook = excel.Workbooks.Add(Type.Missing);

            excelsheet = (Microsoft.Office.Interop.Excel.Worksheet)excelworkbook.ActiveSheet;
            excelsheet.Name = "dataToExcel";

            excelsheet.Cells[1, 1] = "test";
            
            excelworkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypeXPS,
                                                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                                Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard,
                                                true, true, 1, 10, false);
        }

it should create a Excel file, with one sheet in it, and had in cell[1, 1] a "test" string, then export it in my documents.

Problem is I have no file, but also no error, or message of the export achieved/failed. The code runs, and do not crash.

Any idea there my problem could be?

Thanks

Upvotes: 0

Views: 300

Answers (1)

BIJIN PALAKKAL
BIJIN PALAKKAL

Reputation: 186

instead of Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

try specifying a file name too,

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\mytestfile.xlsx"

Upvotes: 1

Related Questions