En iyi yanıtlayıcılar
Excel'e iki sayfaya birden veri yazmak

Soru
-
Merhaba Arkadaşlar,
C#, Bir exel listeli oluşturuyorum ürünler ve barkodlar diye bir exel içerisinde iki adet sayfa olması gerekiyor ürünleri exel içerisindeki ürünler sayfasına atıyorum barkodlar sayfasına ise sadece başlıklar geliyor fakat verileri atamıyorum yardımcı olabilir misiniz
public IEnumerable<string> ExportExcel<T>(string path, List<T> list, bool urn=true) { Microsoft.Office.Interop.Excel.Application xlApp; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet urunsayfa; Microsoft.Office.Interop.Excel.Worksheet brksayfa; object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Range columnRange; xlApp = new Microsoft.Office.Interop.Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); urunsayfa = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); urunsayfa.Name = "Ürün Listesi"; urunsayfa.Cells[1, 2] = "Ürün Adı"; urunsayfa.Cells[1, 3] = "Ürün Kodu"; urunsayfa.Cells[1, 4] = "Kısım No"; urunsayfa.Cells[1, 5] = "Firma ID"; columnRange = urunsayfa.get_Range("A1", "A1"); columnRange.EntireRow.Font.Bold = true; columnRange.EntireRow.Font.Color = Color.Red; #region Barkod brksayfa = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2); brksayfa.Name = "Barkod Listesi"; brksayfa.Cells[1, 1] = "urun_kodu"; brksayfa.Cells[1, 2] ="barkod" ; columnRange = brksayfa.get_Range("A1", "A1"); columnRange.EntireRow.Font.Bold = true; columnRange.EntireRow.Font.Color = Color.Red; #endregion int i = 0; if (urn) { foreach (T t in list) { int j = 0; foreach (PropertyInfo info in typeof(T).GetProperties()) { if (!IsNullableType(info.PropertyType)) { urunsayfa.Cells[i + 2, j + 1] = info.GetValue(t, null); } else { if (info.GetValue(t, null) != null) { urunsayfa.Cells[i + 2, j + 1] = info.GetValue(t, null); } else { urunsayfa.Cells[i + 2, j + 1] = DBNull.Value; } } j++; } i++; yield return "Ürünler Aktarılıyor :" + i + "/" + list.Count; } } else { foreach (T t in list) { int j = 0; foreach (PropertyInfo info in typeof(T).GetProperties()) { if (!IsNullableType(info.PropertyType)) { brksayfa.Cells[i + 2, j + 1] = info.GetValue(t, null); } else { if (info.GetValue(t, null) != null) { brksayfa.Cells[i + 2, j + 1] = info.GetValue(t, null); } else { brksayfa.Cells[i + 2, j + 1] = DBNull.Value; } } j++; } i++; yield return "Barkodlar Aktarılıyor :" + i + "/" + list.Count; } } xlWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue,
misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue,
misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(urunsayfa); releaseObject(xlWorkBook); releaseObject(xlApp); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; } finally { GC.Collect(); } } private static bool IsNullableType(Type type) { return (type == typeof(string) || type.IsArray || (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))); }
- Düzenleyen csharpogreniyorum 25 Nisan 2018 Çarşamba 15:15
- Değiştirilmiş Tür Kyamuran SalibryamMicrosoft contingent staff, Moderator 27 Nisan 2018 Cuma 11:53
- Değiştirilmiş Tür csharpogreniyorum 3 Mayıs 2018 Perşembe 09:44 Soruyu kapatmam gerekiyor
Yanıtlar
-
Sorunumu çözdüm yinede teşekkür ederim.
- Yanıt Olarak İşaretleyen csharpogreniyorum 3 Mayıs 2018 Perşembe 09:46