VS C# 2008导入到Excel 2007

string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal).ToString();//获取当前用的MyDocument文件夹

//保存到excel 2007中
        private void saveToExcel()
        {
            try
            {
                object m_objOpt = System.Reflection.Missing.Value;

                Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook workBook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

                this.outBox(excelApp, workBook);

                if (File.Exists(path + @"\MobileBackup.xlsx"))
                {
                    DialogResult result = MessageBox.Show("该备份文件已经存在,是否覆盖原文件?", "信息提示",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        File.Delete(path + @"\MobileBackup.xlsx");

                        workBook.SaveCopyAs(path + @"\MobileBackup.xlsx");

                        this.SHTMain_Load(null, null);

                        this.statusBarPanel1.Text = "导出数据成功";
                    }
                    else
                    {
                        OpenFileDialog dialog = new OpenFileDialog();
                        dialog.Filter = "Excel文件(*.xlsx,*.xls)|*.xlsx;*xls";//设置打开文件的格式
                        dialog.InitialDirectory = path;//指定初始化路径

                        DialogResult DResult = dialog.ShowDialog();

                        if (dialog.FileName.Trim() != "")
                        {
                            if (DResult == DialogResult.OK)
                            {
                                workBook.SaveCopyAs(dialog.FileName);

                                this.SHTMain_Load(null, null);

                                this.label8.Text = path + "\\" + dialog.FileName;

                                this.statusBarPanel1.Text = "导出数据成功";
                            }
                        }
                        else
                        {
                            this.statusBarPanel1.Text = "请输入您要保存的文件的文件名";
                        }
                    }
                }
                else
                {
                    workBook.SaveCopyAs(path + @"\MobileBackup.xlsx");

                    this.SHTMain_Load(null, null);

                    this.label8.Text = path + @"\MobileBackup.xlsx";

                    this.statusBarPanel1.Text = "导出数据成功";
                    //workBook.SaveAs(path + @"\MobileBackup.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                }

                workBook.Close(false, m_objOpt, m_objOpt);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);

                this.statusBarPanel2.Text = "";

                //显示excel
                //excelApp.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("您当前的电脑上没有安装Excel 2007,请安装上了以后再导出数据吧!", "错误信息提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

//发件箱
        private void outBox(Microsoft.Office.Interop.Excel.Application excelApp, Microsoft.Office.Interop.Excel.Workbook workBook)
        {
            Microsoft.Office.Interop.Excel.Worksheet outBoxWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[1];
            outBoxWorkSheet.Name = "OutBox";

            excelApp.Cells[1, 1] = "发件箱";
            excelApp.Cells[2, 1] = "编号";
            //显示表头
            for (int i = 0; i < this.sHToutBoxDataGridView.ColumnCount; i++)
            {
                excelApp.Cells[2, i + 2] = this.sHToutBoxDataGridView.Columns[i].HeaderText;
            }
            //显示内容
            int outBoxCount = this.sHToutBoxDataGridView.Rows.Count;
            for (int i = 0; i < outBoxCount; i++)
            {
                excelApp.Cells[i + 3, 1] = i + 0;

                for (int j = 0; j < this.sHToutBoxDataGridView.ColumnCount; j++)
                {
                    if (this.sHToutBoxDataGridView[j, i].ValueType == typeof(string))
                    {
                        excelApp.Cells[i + 3, j + 2] = "'" + this.sHToutBoxDataGridView[j, i].Value.ToString();
                    }
                    else
                    {
                        excelApp.Cells[i + 3, j + 2] = this.sHToutBoxDataGridView[j, i].Value.ToString();
                    }

                }
            }
        }

你可能感兴趣的:(Excel)