private void ExportExcel() { IEnumerable <Object> dtSV = null; var MaKhoa = srcKhoa.EditValue.ToString(); if (MaKhoa == "admin") { dtSV = SinhVienRepository.GetAllSinhVienToExportExcel(srcKhoa.EditValue.ToString()); } else { dtSV = SinhVienRepository.GetSinhVienToExportExcel(srcKhoa.EditValue.ToString()); } DataTable dt = ConvertDataTable.LINQToDataTable(dtSV); SaveExcel.FileName = "DanhSachSV"; if (SaveExcel.ShowDialog() == DialogResult.OK) { string FolderPath = SaveExcel.FileName; using (XLWorkbook wb = new XLWorkbook()) { var ws = wb.Worksheets.Add(dt, "Temp"); wb.SaveAs(FolderPath + ".xlsx"); } XtraMessageBox.Show("Xuất Excel thành công !", "Thông báo", MessageBoxButtons.OK); } }
private void Button4_Click(object sender, EventArgs e) { DialogResult DResult = SaveExcel.ShowDialog(); if (DResult != DialogResult.Cancel && DResult != DialogResult.Abort) { string filePath = SaveExcel.FileName; Class1.ExportExcelData(filePath, chart1); } }
private void btnOutToExcel_Click(object sender, EventArgs e) { string SaveFilename = ""; if (SaveExcel.ShowDialog() != DialogResult.OK) { return; } else { SaveFilename = SaveExcel.FileName; } string errstr = ""; DataGridViewExportToExcel(dsShow.Tables[0], SaveFilename, ref errstr); }
private void btnOutToExcel_Click(object sender, EventArgs e) { string SaveFilename = ""; if (SaveExcel.ShowDialog() != DialogResult.OK) { return; } else { SaveFilename = SaveExcel.FileName; } string errstr = ""; // DataGridViewExportToExcel(dsShow.Tables[0], SaveFilename, ref errstr); //} //public static bool DataGridViewExportToExcel(DataTable mDataTable, String strFileName, ref String strMsg) //{ // strMsg = ""; // // 创建Excel对象 // Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); // //Microsoft.Office.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); // if (xlApp == null) // { // strMsg = "Excel无法启动"; // return false; // } // // 创建Excel工作薄 // Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true); // Microsoft.Office.Interop.Excel.Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets[1]; // Microsoft.Office.Interop.Excel.Range range = null; // /* // // 设置标题 // range = xlSheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,ts.GridColumnStyles.Count]); // range.MergeCells = true; // xlApp.ActiveCell.FormulaR1C1 = p_ReportName; // xlApp.ActiveCell.Font.Size = 20; // xlApp.ActiveCell.Font.Bold = true; // xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter; // */ // // 列索引,行索引,总列数,总行数 // int colIndex = 0; // int RowIndex = 0; // int colCount = mDataTable.Columns.Count; // int RowCount = mDataTable.Rows.Count + 1; // // 创建缓存数据 // object[,] objData = new object[RowCount + 1, colCount]; // // 获取列标题 // foreach (DataColumn dc in mDataTable.Columns) // { // objData[RowIndex, colIndex++] = dc.ColumnName; // } // // 获取数据 // for (RowIndex = 1; RowIndex < RowCount; RowIndex++) // { // for (colIndex = 0; colIndex < colCount; colIndex++) // { // objData[RowIndex, colIndex] = mDataTable.Rows[RowIndex - 1][colIndex].ToString(); // } // } // // 写入Excel // //((Excel.Range)xlSheet.Columns["A:A ",System.Reflection.Missing.Value]).ColumnWidth = 16; // range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[RowCount, colCount]); // range.Value2 = objData; // range = (Microsoft.Office.Interop.Excel.Range)xlSheet.Columns["A:B", System.Type.Missing]; // range.ColumnWidth = 20; // // 保存 // try // { // xlBook.Saved = true; // xlBook.SaveCopyAs(strFileName); // MessageBox.Show("数据转存为Excel成功"); // } // catch (Exception ee) // { // strMsg = ee.Message; // return false; // } // finally // { // range = null; // xlSheet = null; // xlBook = null; // xlApp.Quit(); // xlApp = null; // GC.Collect(); // } // return true; }