Windows表单应用程序每次导出excel时都会替换

时间:2015-09-01 06:46:39

标签: c# excel winforms localdb

我编写了以下代码,用于将本地数据库数据导出到excel文件。事实是它运作正常,但我只想用实际日期或月份,如八月,或类似的东西出口它。除此之外,真正重要的问题是它每次都会替换以前导出的excel文件。我怎么能改变这个东西?谢谢!

private void button3_Click_1(object sender, EventArgs e)
        {
             var connString = (@"Data Source=" + System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + @"\Angajati.sdf");
            using (var conn = new SqlCeConnection(connString))
            {
                try
                {
                    conn.Open();
                    var query = "SELECT * FROM info ";
                    var command = new SqlCeCommand(query, conn);
                    var dataAdapter = new SqlCeDataAdapter(command);
                    var dataTable = new DataTable();
                    dataAdapter.Fill(dataTable);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;

            object misValue = System.Reflection.Missing.Value;

            Int16 i, j;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            for (i = 0; i <= dataGridView1.RowCount - 2; i++)
            {
                for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                {
                    xlWorkSheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
                }
            }

            //adds column names to excel
            string[] colNames = new string[dataGridView1.Columns.Count];
            int col = 0;

            foreach (DataGridViewColumn dc in dataGridView1.Columns)
                colNames[col++] = dc.HeaderText;

            char lastColumn = (char)(65 + dataGridView1.Columns.Count - 1);

            xlWorkSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
            xlWorkSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
            xlWorkSheet.get_Range("A1", lastColumn + "1").VerticalAlignment
                        = Excel.XlVAlign.xlVAlignCenter;

            xlWorkBook.SaveAs(@"C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\db.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
            MessageBox.Show("Salvat cu succes");
        }



        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

1 个答案:

答案 0 :(得分:1)

如果您不希望每次都覆盖同一个工作簿,那么您需要指定一个不同的文件名而不是硬编码。

使用当前日期时间创建文件名这样的简单通常就足够了(您可以更改格式以适合您):

string fileName = @"db " + DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss") + @".xls";
string filePath = Path.Combine(@"C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag", fileName);

xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

这将生成如下文件:

  

db 01-09-2015 10-32-35.xls

仍然没有明确检查该文件是否存在,但除非您每秒都点击它,否则它不太可能覆盖。