Setelah Anda selesai menjalankan kueri di Pengembang SQL, Anda akan melihat output kueri di bagian bawah layar Anda
Klik kanan pada sel mana pun pada hasil kueri, lalu pilih opsi 'Ekspor...' dari menu tarik-turun
Anda kemudian akan melihat Wisaya Ekspor
Langkah 3. Pilih format Excel dan lokasi untuk mengekspor file Anda
Sekarang Anda harus melakukannya
- Pilih format Excel dari daftar drop-down. Anda dapat memilih format xlsx untuk versi terbaru Excel, atau format xls untuk versi Excel sebelumnya
- Klik tombol 'Browse…' untuk memilih lokasi penyimpanan file Excel
- Tekan tombol ‘Berikutnya >‘ setelah Anda selesai
Langkah 4. Ekspor output kueri ke Excel
Untuk langkah terakhir, klik tombol 'Selesai' untuk mengekspor hasil kueri ke Excel
Kesimpulan
Anda baru saja melihat cara mengekspor hasil kueri Anda ke Excel di SQL Developer. Anda dapat memilih untuk mengekspor hasil kueri ke format xlsx untuk versi Excel yang lebih baru, atau format xls untuk versi Excel sebelumnya
Jika Anda perlu mengekspor hasil kueri Anda ke file CSV, Anda dapat memeriksa panduan berikut yang menjelaskan langkah-langkah untuk mengekspor hasil kueri Anda ke CSV di Pengembang SQL. Alternatifnya, Anda dapat menerapkan spool untuk mencapai tujuan yang sama
Bagi siapa pun yang datang ke sini mencari cara melakukan ini di C #, saya telah mencoba metode berikut dan berhasil di dotnet core 2.0.3 dan entity framework core 2.0.3
Pertama buat kelas model Anda
Kemudian instal paket EPPlus Nuget. (Saya menggunakan versi 4. 0. 5, mungkin akan bekerja untuk versi lain juga. )
Install-Package EPPlus -Version 4.0.5 _Buat kelas ExcelExportHelper_, yang akan berisi logika untuk mengonversi kumpulan data ke baris Excel. Kelas ini tidak memiliki ketergantungan dengan kelas model atau kumpulan data Anda
public class ExcelExportHelper { public static string ExcelContentType { get { return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; } } public static DataTable ListToDataTable(List data) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); DataTable dataTable = new DataTable(); for (int i = 0; i < properties.Count; i++) { PropertyDescriptor property = properties[i]; dataTable.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType); } object[] values = new object[properties.Count]; foreach (T item in data) { for (int i = 0; i < values.Length; i++) { values[i] = properties[i].GetValue(item); } dataTable.Rows.Add(values); } return dataTable; } public static byte[] ExportExcel(DataTable dataTable, string heading = "", bool showSrNo = false, params string[] columnsToTake) { byte[] result = null; using (ExcelPackage package = new ExcelPackage()) { ExcelWorksheet workSheet = package.Workbook.Worksheets.Add(String.Format("{0} Data", heading)); int startRowFrom = String.IsNullOrEmpty(heading) ? 1 : 3; if (showSrNo) { DataColumn dataColumn = dataTable.Columns.Add("#", typeof(int)); dataColumn.SetOrdinal(0); int index = 1; foreach (DataRow item in dataTable.Rows) { item[0] = index; index++; } } // add the content into the Excel file workSheet.Cells["A" + startRowFrom].LoadFromDataTable(dataTable, true); // autofit width of cells with small content int columnIndex = 1; foreach (DataColumn column in dataTable.Columns) { int maxLength; ExcelRange columnCells = workSheet.Cells[workSheet.Dimension.Start.Row, columnIndex, workSheet.Dimension.End.Row, columnIndex]; try { maxLength = columnCells.Max(cell => cell.Value.ToString().Count()); } catch (Exception) //nishanc { maxLength = columnCells.Max(cell => (cell.Value +"").ToString().Length); } //workSheet.Column(columnIndex).AutoFit(); if (maxLength < 150) { //workSheet.Column(columnIndex).AutoFit(); } columnIndex++; } // format header - bold, yellow on black using (ExcelRange r = workSheet.Cells[startRowFrom, 1, startRowFrom, dataTable.Columns.Count]) { r.Style.Font.Color.SetColor(System.Drawing.Color.White); r.Style.Font.Bold = true; r.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; r.Style.Fill.BackgroundColor.SetColor(Color.Brown); } // format cells - add borders using (ExcelRange r = workSheet.Cells[startRowFrom + 1, 1, startRowFrom + dataTable.Rows.Count, dataTable.Columns.Count]) { r.Style.Border.Top.Style = ExcelBorderStyle.Thin; r.Style.Border.Bottom.Style = ExcelBorderStyle.Thin; r.Style.Border.Left.Style = ExcelBorderStyle.Thin; r.Style.Border.Right.Style = ExcelBorderStyle.Thin; r.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black); r.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.Black); r.Style.Border.Left.Color.SetColor(System.Drawing.Color.Black); r.Style.Border.Right.Color.SetColor(System.Drawing.Color.Black); } // removed ignored columns for (int i = dataTable.Columns.Count - 1; i >= 0; i--) { if (i == 0 && showSrNo) { continue; } if (!columnsToTake.Contains(dataTable.Columns[i].ColumnName)) { workSheet.DeleteColumn(i + 1); } } if (!String.IsNullOrEmpty(heading)) { workSheet.Cells["A1"].Value = heading; // workSheet.Cells["A1"].Style.Font.Size = 20; workSheet.InsertColumn(1, 1); workSheet.InsertRow(1, 1); workSheet.Column(1).Width = 10; } result = package.GetAsByteArray(); } return result; } public static byte[] ExportExcel(List data, string Heading = "", bool showSlno = false, params string[] ColumnsToTake) { return ExportExcel(ListToDataTable(data), Heading, showSlno, ColumnsToTake); } }Sekarang tambahkan metode ini di mana Anda ingin membuat file excel, mungkin untuk metode di controller. Anda juga dapat memberikan parameter untuk prosedur tersimpan Anda. Perhatikan bahwa tipe kembalian dari metode ini adalah FileContentResult. Permintaan apa pun yang Anda jalankan, yang penting adalah Anda harus mendapatkan hasilnya dalam List