/// <summary> /// Saves the given DataTable to the given Excel path and file name /// </summary> /// <param name="dt"></param> /// <param name="saveAsPathAndFile"></param> /// <param name="openAfterSave"></param> public static void ExportDataTableToExcel(DataTable dt, string saveAsPathAndFile, bool openAfterSave) { if (String.IsNullOrEmpty(saveAsPathAndFile)) { throw new Exception("Missing saveAsPathAndFile for ExportDataSetToExcel()!"); } Spire.DataExport.XLS.CellExport cellExport = new Spire.DataExport.XLS.CellExport(); cellExport.Register(RegAcctNm, RegAcctKey); Spire.DataExport.XLS.WorkSheet worksheet1 = new Spire.DataExport.XLS.WorkSheet(); worksheet1.DataSource = Spire.DataExport.Common.ExportSource.DataTable; worksheet1.DataTable = dt; worksheet1.StartDataCol = ((System.Byte)(0)); worksheet1.AutoFitColWidth = true; cellExport.Sheets.Add(worksheet1); if (openAfterSave) { cellExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView; } cellExport.SaveToFile(saveAsPathAndFile); Console.WriteLine(saveAsPathAndFile + " was created."); }
private void button2_Click(object sender, EventArgs e) { if (textBox2.Text == "") { MessageBox.Show("Filename Field Empty", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string fileName; Spire.DataExport.XLS.CellExport cellExport = new Spire.DataExport.XLS.CellExport(); Spire.DataExport.XLS.WorkSheet worksheet1 = new Spire.DataExport.XLS.WorkSheet(); worksheet1.DataSource = Spire.DataExport.Common.ExportSource.DataTable; worksheet1.DataTable = this.dataGridView1.DataSource as DataTable; worksheet1.StartDataCol = ((System.Byte)(0)); cellExport.Sheets.Add(worksheet1); cellExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView; fileName = textBox2.Text.ToString() + ".xls"; cellExport.SaveToFile(fileName); } }