1-) C# RMOS - xml veya json veya excel vs yükleme ve kaydetme
1-) JSON YÜKLEME
private void btnPetzYukleJson_Click(object sender, EventArgs e) { gridControl3.DataSource = null; gridView3.Columns.Clear();
using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Title = "Json Seç"; dlg.Filter = "Json Files (*.json;)|*.JSON;";
if (dlg.ShowDialog() == DialogResult.OK) { string strFile = dlg.FileName; string json = File.ReadAllText(strFile);
List<PetzShopModel> shopModels = JsonConvert.DeserializeObject<List<PetzShopModel>>(json);
gridControl3.DataSource = shopModels; gridviewCountYaz(gridView3);
} }
}
|
2-) XML YÜKLEME
private void btnGratisXmlYukle_Click(object sender, EventArgs e) { gridControlGratis.DataSource = null; gridViewGratis.Columns.Clear();
using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Title = "Gratis Xml Seç"; dlg.Filter = "Xml Files (*.xml;)|*.XML;";
if (dlg.ShowDialog() == DialogResult.OK) { string strFile = dlg.FileName; string json = File.ReadAllText(strFile);
List<GenelModel> genelModels = loadXml<List<GenelModel>>(strFile);
gridControlGratis.DataSource = genelModels; gridviewCountYaz(gridViewGratis);
} } }
public T loadXml<T>(string path) { using (var stream = new FileStream(path, FileMode.Open)) { var XML = new XmlSerializer(typeof(T)); return (T)XML.Deserialize(stream); } }
|
3-) DEVEXPRESS GRİDVİEW COUNT YAZMA
public void gridviewCountYaz(GridView grid) { if (grid.Columns.Count > 0) { grid.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count; grid.Columns[0].SummaryItem.FieldName = grid.Columns[0].FieldName; grid.Columns[0].SummaryItem.DisplayFormat = "{0:n0}"; grid.UpdateTotalSummary(); } grid.BestFitColumns(); }
|
4-) JSON KAYDETME
public void jsonKaydet(GridControl gridControl) { try { var dataTable = gridControl.DataSource; string json = JsonConvert.SerializeObject(dataTable, Newtonsoft.Json.Formatting.Indented);
SaveFileDialog sf = new SaveFileDialog(); sf.Filter = "Json files (*.json)|*.json"; sf.FilterIndex = 2; sf.RestoreDirectory = true; if (sf.ShowDialog() == DialogResult.OK) { File.WriteAllText(sf.FileName, json);
System.Diagnostics.Process.Start(Path.GetDirectoryName(sf.FileName));
}
} catch (Exception ex) { MessageBox.Show("HATA " + ex.Message); } }
|
5-) XML KAYDETME
private void btnXmlKaydet_Click(object sender, EventArgs e) { List<AnaModel.Product> model = gridControl1.DataSource as List<AnaModel.Product>; XmlDocument xmlDocument = SerializeObjectToXML(model, "asd"); xmlDocument.InnerXml = xmlDocument.InnerXml.ToString().Replace("version=\"1.0\"", "version=\"1.0\" encoding=\"ISO-8859-9\""); xmlDocument.InnerXml = xmlDocument.InnerXml.ToString().Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", ""); xmlDocument.InnerXml = xmlDocument.InnerXml.ToString().Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
xmlDocument.InnerXml = xmlDocument.InnerXml.ToString().Replace("ArrayOfProduct", "Products"); SaveFileDialog sf = new SaveFileDialog(); sf.Filter = "XML files (*.xml)|*.xml"; sf.FilterIndex = 2; sf.RestoreDirectory = true; if (sf.ShowDialog() == DialogResult.OK) {
xmlDocument.Save(sf.FileName);
System.Diagnostics.Process.Start(Path.GetDirectoryName(sf.FileName));
}
}
public static XmlDocument SerializeObjectToXML(object obj, string sElementName) { XmlSerializer serializer = new XmlSerializer(obj.GetType()); XmlDocument xmlDoc = new XmlDocument(); using (MemoryStream ms = new MemoryStream()) { serializer.Serialize(ms, obj); ms.Position = 0; xmlDoc.Load(ms); } return xmlDoc; }
|
6-) EXCEL YÜKLEME
private void button1_Click(object sender, EventArgs e) { excelYukle(); }
public void excelYukle() { gridControl1.DataSource = null; gridView1.Columns.Clear();
using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Title = "Excel Seç"; dlg.Filter = "Excel Files (*.xls;*.xlsx;)|*.XLS;*.XLSX;";
if (dlg.ShowDialog() == DialogResult.OK) { string strFile = dlg.FileName; gridControl1.DataSource = LoadExcel(strFile); } } }
private System.Data.DataTable LoadExcel(string strFile, string sheetName = "") { System.Data.DataTable dtXLS = null; try { string strConnectionString = ""; if (strFile.Trim().EndsWith(".xlsx")) { strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", strFile); } else if (strFile.Trim().EndsWith(".xls")) { strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", strFile); } else if (strFile.Trim().EndsWith(".xlsm")) { strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Macro;HDR=Yes;IMEX=1\";", strFile); } OleDbConnection SQLConn = new OleDbConnection(strConnectionString); SQLConn.Open();
if (sheetName == "") { var dtSchema = SQLConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); sheetName = dtSchema.Rows[0].Field<string>("TABLE_NAME").Replace("$",""); } dtXLS = new System.Data.DataTable(sheetName);
OleDbDataAdapter SQLAdapter = new OleDbDataAdapter(); string sql = "SELECT * FROM [" + sheetName + "]"; OleDbCommand selectCMD = new OleDbCommand(sql, SQLConn); SQLAdapter.SelectCommand = selectCMD; SQLAdapter.Fill(dtXLS); SQLConn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return dtXLS; } |
7-) EXCEL KAYDETME (filtrelenmişleri bu iyi oldu)
public void yazdir(GridControl gridControl) { using (SaveFileDialog saveDialog = new SaveFileDialog()) { saveDialog.Filter = "Excel (2010) (.xlsx)|*.xlsx|Excel (2003)(.xls)|*.xls|RichText File (.rtf)|*.rtf |Pdf File (.pdf)|*.pdf |Html File (.html)|*.html"; if (saveDialog.ShowDialog() != DialogResult.Cancel) { string exportFilePath = saveDialog.FileName; string fileExtenstion = new FileInfo(exportFilePath).Extension;
switch (fileExtenstion) { case ".xls": gridControl.ExportToXls(exportFilePath); break; case ".xlsx": gridControl.ExportToXlsx(exportFilePath); break; case ".rtf": gridControl.ExportToRtf(exportFilePath); break; case ".pdf": gridControl.ExportToPdf(exportFilePath); break; case ".html": gridControl.ExportToHtml(exportFilePath); break; case ".mht": gridControl.ExportToMht(exportFilePath); break; default: break; }
//string dosyaKonum = Path.GetDirectoryName(Application.ExecutablePath);
string basePath = Path.GetDirectoryName(exportFilePath);
var p = new Process(); p.StartInfo = new ProcessStartInfo(basePath) { UseShellExecute = true }; p.Start(); } } }
|
10-) Yazı bozuk gelirse şunlara dikkat et [Encoding.Default]
File.WriteAllText(def, json.Replace("Products", "ArrayOfProduct"),Encoding.Default);
string json = File.ReadAllText(strFile,Encoding.Default);
11-) ESKİ EXCEL YÜKLEME
ilk sayfa ismini alma
SQLConn.Open(); var dtSchema = SQLConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); sheetName = dtSchema.Rows[0].Field<string>("TABLE_NAME"); dtXLS = new System.Data.DataTable(sheetName); |
private void button1_Click(object sender, EventArgs e) { excelYukle(); }
public void excelYukle() { gridControl1.DataSource = null; gridView1.Columns.Clear();
using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Title = "Excel Seç"; dlg.Filter = "Excel Files (*.xls;*.xlsx;)|*.XLS;*.XLSX;";
if (dlg.ShowDialog() == DialogResult.OK) { string strFile = dlg.FileName; gridControl1.DataSource = LoadExcel(strFile, "Worksheet"); } } }
private System.Data.DataTable LoadExcel(string strFile, string sheetName) { System.Data.DataTable dtXLS = new System.Data.DataTable(sheetName); try { string strConnectionString = ""; if (strFile.Trim().EndsWith(".xlsx")) { strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", strFile); } else if (strFile.Trim().EndsWith(".xls")) { strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", strFile); } else if (strFile.Trim().EndsWith(".xlsm")) { strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Macro;HDR=Yes;IMEX=1\";", strFile); } OleDbConnection SQLConn = new OleDbConnection(strConnectionString); SQLConn.Open(); OleDbDataAdapter SQLAdapter = new OleDbDataAdapter(); string sql = "SELECT * FROM [" + sheetName + "$]"; OleDbCommand selectCMD = new OleDbCommand(sql, SQLConn); SQLAdapter.SelectCommand = selectCMD; SQLAdapter.Fill(dtXLS); SQLConn.Close(); } catch (Exception) { throw; } return dtXLS; }
|