从xlsm模板导出到excel

时间:2015-03-17 12:39:54

标签: c# windows-applications epplus

要求


我需要在C#中创建一个Windows应用程序,其中输出是一个excel文件(xlsm),它是从xlsm格式的模板(包含宏)创建的。

在模板文件“IneTemplate.xlsm”中有一个隐藏的工作表“Data”。我必须从数据库中填入数据(没有列的标题,只有数据),并使用“保存文件”对话框进行保存。


到目前为止我做了什么?


我有一个按钮。在按钮单击中我写了这个。

using OfficeOpenXml;
    using (var ms = new MemoryStream())
                {
                    //Default filename for new excel
                    String newFileName = string.Concat("ExcelExport", '(',
                                   DateTime.Now.ToString("dd-MM-yyyy h:mm:ss tt")
                                   .Replace(':', '_')
                                   .Replace('/', '-')
                                   .Replace(' ', '_'), ')', ".xlsm");

                    FileInfo existingFile = new FileInfo(Environment.CurrentDirectory + @"\App_Data\IneTemplate.xlsm");

                    if (existingFile.Exists)
                    {

                        using (var MyExcel = new ExcelPackage(existingFile))
                        {
                            ExcelWorksheet worksheet = MyExcel.Workbook.Worksheets["Data"]; 
                            int tripCount = 1;

                            //I have few trip data in a checked list box which I need to fill in the first column (column "A")
                            foreach (object item in clbTrip.CheckedItems)
                            {
                                DropDown trip = (DropDown)item;
                                worksheet.Cells[tripCount, 1].Value = trip.Value;
                                tripCount++;
                            }

                            int stopcount = 2;
                            int rowCount = 1;

                            //I have to fill the remaining columns with stops from each Trip in each column.(stops from Trip1 in column "B", from Trip 2 in "C" and so on) 
                            foreach (object item in clbTrip.CheckedItems)
                            {
                                DropDown trip = (DropDown)item;

                                //Get the stops in a Trip from database
                                DataSet dsStopNames = objTrip.GetStopNames(trip.Id);
                                foreach (DataRow row in dsStopNames.Tables[0].Rows)
                                {
                                    worksheet.Cells[rowCount, stopcount].Value = Convert.ToString(row["StopAliasName"]);
                                    rowCount++;
                                }
                                stopcount++;
                                rowCount = 1;
                            }
                            try
                            {
                                //Create a save file Dialog
                                SaveFileDialog saveFileDialogExcel = new SaveFileDialog();
                                saveFileDialogExcel.Filter = "Excel files (*.xlsm)|*.xlsm";
                                saveFileDialogExcel.Title = "Export Excel File To";
                                saveFileDialogExcel.FileName = newFileName;

                                if (saveFileDialogExcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                {
                                    if (saveFileDialogExcel.FileName != "")
                                    {
                                        string path = saveFileDialogExcel.FileName;
                                        MyExcel.SaveAs(ms);
                                        File.WriteAllBytes(path, ms.ToArray());
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                MessageBox.Show("Exception Occured While Saving , Check Whether the file is open");
                            }

                        }
                    }
                }

其他信息


  1. 使用EPPlus进行Excel生成。
  2. 系统中未安装Microsoft Office。

  3. 问题


    创建了一个文件。但是当我打开时,它的显示文件格式不正确或已损坏或其他东西。


    还有其他方法可以达到这个目的吗? EPPlus能否处理xlsm文件?

    请帮助。

0 个答案:

没有答案
相关问题