且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中使用OpenXml创建/打开Excel文件

更新时间:2023-02-08 14:05:19

保存文件对话框.它将提示用户选择保存文件的位置.之后,您可以使用saveFileDialog.FileName.ToString()属性获取完整路径. 请参见下面的示例代码:

se the Save file dialog. It will prompts the user to select a location for saving a file. After that you can use saveFileDialog.FileName.ToString() property to get the full path. See the sample code below:

//Save a file in a particular format as specified in the saveAsType parameter
     private void OpenSaveFileDialog(int saveAsType)
     {
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
        saveFileDialog.Filter = "CSV|*.csv|Excel|*.xlsx";
        saveFileDialog.FilterIndex = saveAsType;
        saveFileDialog.Title = "Save Data";
        saveFileDialog.FileName = "My File";
        saveFileDialog.ShowDialog();

        if (saveFileDialog.FileName != "")
        {
            //File Path =   m_fileName         
             m_fileName = saveFileDialog.FileName.ToString();
             //FilterIndex property is one-based.
             switch (saveFileDialog.FilterIndex)
             {
                case 1:
                    m_fileType = 1;
                    break;
                case 2:
                    m_fileType = 2;
                    break;
              }
        }
      }

参考: http://msdn.microsoft .com/en-us//library/system.windows.forms.savefiledialog.aspx