简体   繁体   English

c#数据到excel导出不导出

[英]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.我目前正在使用一些 c# 并且有一些我想导出为 Excel 文件的数据。

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.它应该创建一个 Excel 文件,其中有一张纸,并在单元格 [1, 1] 中有一个“测试”字符串,然后将其导出到我的文档中。

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谢谢

instead of Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)而不是 Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

try specifying a file name too,也尝试指定文件名,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM