using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.IO; using System.Xml; using System.Text.RegularExpressions; using Excel = Microsoft.Office.Interop.Excel; public class Export : Object { public static int GetElapseDay(string fromDateStr, string toDateStr ) { //Ãâ¹ß ¸ñÇ¥ Regex fromRegex = new System.Text.RegularExpressions.Regex("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"); Match fromMatch = fromRegex.Match(fromDateStr); string fromStr = fromMatch.Value; string[] fromDateArr = fromStr.Split('-'); int fromYear = Int32.Parse(fromDateArr[0]); int fromMonth = Int32.Parse(fromDateArr[1]); int fromDay = Int32.Parse(fromDateArr[2]); int fromSum = fromYear * 12 * 30 + fromMonth * 30 + fromDay; //¸ñÇ¥ ³¯Â¥ Regex toRegex = new System.Text.RegularExpressions.Regex("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"); Match toMatch = toRegex.Match(toDateStr); string toStr = toMatch.Value; string[] toDateArr = toStr.Split('-'); int toYear = Int32.Parse(toDateArr[0]); int toMonth = Int32.Parse(toDateArr[1]); int toDay = Int32.Parse(toDateArr[2]); int toSum = toYear * 12 * 30 + toMonth * 30 + toDay; return (toSum - fromSum); } public static bool RunSVN() { string xmlFileName = "loginput.xml"; if (File.Exists(xmlFileName)) File.Delete(xmlFileName); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "svn.exe"; p.StartInfo.Arguments = "log --xml https://nebuladevice.svn.sourceforge.net/svnroot/nebuladevice/trunk/nebula2/update.py"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.StandardOutputEncoding = Encoding.UTF8; p.Start(); // ÆÄÀÏ ÀúÀå using (StreamWriter pFileWriter = new StreamWriter(xmlFileName, false, Encoding.UTF8))//Encoding.Default)) { pFileWriter.WriteLine(p.StandardOutput.ReadToEnd()); } return true; } public static bool PrintfXml() { string xmlFileName = "loginput.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFileName); XmlElement root = xmlDoc.DocumentElement; XmlNodeList nodeList = root.SelectNodes("/log/logentry"); foreach (XmlNode logEntry in nodeList) { XmlAttributeCollection xmlAttr = logEntry.Attributes; for (int j = 0; j < xmlAttr.Count; j++) { if (xmlAttr[j].Name == "revision") Console.Write(": " + xmlAttr[j].Value + "\t\t\n"); } for (int j = 0; j < logEntry.ChildNodes.Count; j++) { if (logEntry.ChildNodes[j].Name == "author") Console.Write(": " + logEntry.ChildNodes[j].InnerText + "\t\t\n"); if (logEntry.ChildNodes[j].Name == "date") { string inputDate = logEntry.ChildNodes[j].InnerText; Console.Write(": " + inputDate + "\t\t\n"); //³¯Â¥¸¸ ÇÊÅ͸µ Regex regex = new System.Text.RegularExpressions.Regex("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"); Match match = regex.Match(inputDate); Console.Write(": " + match.Value + "\t\t\n"); } if (logEntry.ChildNodes[j].Name == "msg") { string msgStr = logEntry.ChildNodes[j].InnerText; Console.Write(": " + msgStr + "\t\t\n"); string newStr = msgStr.Replace(',', '\''); Console.Write(": " + newStr + "\t\t\n"); } } Console.WriteLine("-----------------------"); } return true; } public static bool SaveCSV(bool bAll) { string xmlFileName = "loginput.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFileName); XmlElement root = xmlDoc.DocumentElement; XmlNodeList nodeList = root.SelectNodes("/log/logentry"); StreamWriter file = null; if (bAll == true) file = new StreamWriter("logAll.csv", false, Encoding.Default); else file = new StreamWriter("logTag.csv", false, Encoding.Default); file.Write("revision,"); file.Write("author,"); file.Write("date,"); file.WriteLine("msg"); foreach (XmlNode logEntry in nodeList) { bool bTag = false; string revisionStr = null, authorStr = null, dateStr = null, msgStr = null; XmlAttributeCollection xmlAttr = logEntry.Attributes; for (int j = 0; j < xmlAttr.Count; j++) { if (xmlAttr[j].Name == "revision") revisionStr = xmlAttr[j].Value + ","; //file.Write(xmlAttr[j].Value + ","); } for (int j = 0; j < logEntry.ChildNodes.Count; j++) { if (logEntry.ChildNodes[j].Name == "author") authorStr = logEntry.ChildNodes[j].InnerText + ","; //file.Write(logEntry.ChildNodes[j].InnerText + ","); if (logEntry.ChildNodes[j].Name == "date") { string inputDate = logEntry.ChildNodes[j].InnerText; //³¯Â¥¸¸ ÇÊÅ͸µ Regex regex = new System.Text.RegularExpressions.Regex("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"); Match match = regex.Match(inputDate); dateStr = match.Value + ","; //file.Write(match.Value + ","); } if (logEntry.ChildNodes[j].Name == "msg") { string newStr0 = logEntry.ChildNodes[j].InnerText; string newStr1 = newStr0.Replace(',', '\''); string newStr2 = newStr1.Replace('\r', ' '); msgStr = newStr2.Replace('\n', ' '); //file.WriteLine(msgStr); if (bAll == false) { string tagStr = msgStr; Regex regex = new System.Text.RegularExpressions.Regex("[^\\w!@$%^&*()-_=+\\|]##|^##"); Match match = regex.Match(tagStr); string str = match.Value; if (str.Length > 1) bTag = true; } } } if (revisionStr != null) { if (bAll == true || bTag == true) { file.Write(revisionStr); file.Write(authorStr); file.Write(dateStr); file.WriteLine(msgStr); } } } file.Flush(); file.Close(); return true; } public static bool SaveXML(bool bAll) { string xmlFileName = "loginput.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFileName); XmlElement root = xmlDoc.DocumentElement; XmlNodeList nodeList = root.SelectNodes("/log/logentry"); foreach (XmlNode logEntry in nodeList) { bool bTag = false; XmlNode parentNode = null; XmlAttributeCollection xmlAttr = logEntry.Attributes; for (int j = 0; j < xmlAttr.Count; j++) { if (xmlAttr[j].Name == "revision") { parentNode = logEntry.ParentNode; } } for (int j = 0; j < logEntry.ChildNodes.Count; j++) { if (logEntry.ChildNodes[j].Name == "date") { string inputDate = logEntry.ChildNodes[j].InnerText; //³¯Â¥¸¸ ÇÊÅ͸µ Regex regex = new System.Text.RegularExpressions.Regex("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"); Match match = regex.Match(inputDate); logEntry.ChildNodes[j].InnerText = match.Value; } if (logEntry.ChildNodes[j].Name == "msg") { string msgStr = logEntry.ChildNodes[j].InnerText; Regex regex = new System.Text.RegularExpressions.Regex("[^\\w!@$%^&*()-_=+\\|]##|^##"); Match match = regex.Match(msgStr); string str = match.Value; if (str.Length > 1) bTag = true; } } if (bTag == false && bAll == false) { parentNode.RemoveChild(logEntry); } } if (bAll) xmlDoc.Save("logAll.xml"); else xmlDoc.Save("logTag.xml"); return true; } public static bool SaveExcel(bool bAll) { string xmlFileName = "loginput.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFileName); XmlElement root = xmlDoc.DocumentElement; XmlNodeList nodeList = root.SelectNodes("/log/logentry"); Excel.Application excelApp = new Excel.Application(); Excel.Workbook wb = excelApp.Workbooks.Add(true); Excel._Worksheet workSheet = wb.Worksheets.get_Item(1) as Excel._Worksheet; workSheet.Cells[1, 1] = "revision"; workSheet.Cells[1, 2] = "author"; workSheet.Cells[1, 3] = "date"; workSheet.Cells[1, 4] = "msg"; int nIndex = 1; foreach (XmlNode logEntry in nodeList) { bool bTag = false; XmlNode parentNode = null; string revisionStr = null, authorStr = null, dateStr = null, msgStr = null; XmlAttributeCollection xmlAttr = logEntry.Attributes; for (int j = 0; j < xmlAttr.Count; j++) { if (xmlAttr[j].Name == "revision") { ++nIndex; revisionStr = xmlAttr[j].Value; //file.Write(xmlAttr[j].Value + ","); } } for (int j = 0; j < logEntry.ChildNodes.Count; j++) { if (logEntry.ChildNodes[j].Name == "author") authorStr = logEntry.ChildNodes[j].InnerText; //file.Write(logEntry.ChildNodes[j].InnerText + ","); if (logEntry.ChildNodes[j].Name == "date") { string inputDate = logEntry.ChildNodes[j].InnerText; //³¯Â¥¸¸ ÇÊÅ͸µ Regex regex = new System.Text.RegularExpressions.Regex("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"); Match match = regex.Match(inputDate); dateStr = match.Value; //file.Write(match.Value + ","); } if (logEntry.ChildNodes[j].Name == "msg") { string newStr = logEntry.ChildNodes[j].InnerText; if (bAll == false) { Regex regex = new System.Text.RegularExpressions.Regex("[^\\w!@$%^&*()-_=+\\|]##|^##"); Match match = regex.Match(newStr); string str = match.Value; if (str.Length > 1) bTag = true; } string newStr1 = newStr.Replace("##", ""); if (newStr1[0] == '=') msgStr = newStr1.Remove(0, 1); else msgStr = newStr1; } } if (revisionStr != null) { if (bAll == true || bTag == true) { workSheet.Cells[nIndex, 1] = revisionStr; workSheet.Cells[nIndex, 2] = authorStr; workSheet.Cells[nIndex, 3] = dateStr; workSheet.Cells[nIndex, 4] = msgStr; } } } string fileName; if(bAll) fileName = "logAll.xls"; else fileName = "logTag.xls"; if (File.Exists(fileName)) File.Delete(fileName); ExcelDispose(fileName, excelApp, wb, workSheet); return true; } //¿¢¼¿ ÀúÀå¹× ¸Þ¸ð¸® ÇØÁ¦ private static void ExcelDispose(string fileName, Excel.Application excelApp, Excel.Workbook wb, Excel._Worksheet workSheet) { string runPathStr = AppDomain.CurrentDomain.BaseDirectory; runPathStr += fileName; wb.SaveAs(runPathStr, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); wb.Close(Type.Missing, Type.Missing, Type.Missing); excelApp.Quit(); releaseObject(excelApp); releaseObject(workSheet); releaseObject(wb); } #region ¸Þ¸ð¸®ÇØÁ¦ private static void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception e) { obj = null; } finally { GC.Collect(); } } #endregion }