using Excel = Microsoft.Office.Interop.Excel;

 

ExcelSaveDialog.Title = "저장";
            ExcelSaveDialog.CreatePrompt = true;
            ExcelSaveDialog.OverwritePrompt = true;
            ExcelSaveDialog.FileName = dirPath.Substring(0, 4) + "년" + dirPath.Substring(4, 2) + "월" + dirPath.Substring(6, 2) + "일" + dirPath.Substring(8, 2) + "시" + dirPath.Substring(10, 2) + "분수검로그";
            ExcelSaveDialog.DefaultExt = "xls";
            ExcelSaveDialog.Filter = "Excel files(*.xls)|*.xls";
            ExcelSaveDialog.InitialDirectory = "C:\\HRDTMS\\받은자료";
           
            DialogResult result = ExcelSaveDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    object missingType = Type.Missing;
                    FileInfo Logtxt = new FileInfo("C:\\HRDTMS\\SET_LOG\\" + CurDate + "_Log.txt");
                    if (Logtxt.Exists)
                    {
                        Excel.Application xls = new Excel.Application();
                        Excel._Workbook wbook = (Excel._Workbook)(xls.Workbooks.Add(Missing.Value));
                        Excel._Worksheet wSheet = (Excel._Worksheet)wbook.ActiveSheet;
                        xls.Visible = false;

                        StreamReader sr = new StreamReader("C:\\HRDTMS\\SET_LOG\\" + CurDate + "_Log.txt", System.Text.Encoding.Default);
                        int i = 0;
                        while (true)
                        {
                            string log = sr.ReadLine();
                            if (log == null)
                            {
                                break;
                            }
                            string[] token = log.Split('|');
                            i++;

                            if (i == 1)
                            {
                                wSheet.Cells[i, 1] = "시간";
                                wSheet.Cells[i, 2] = "PC번호";
                                wSheet.Cells[i, 3] = "비번호";
                                wSheet.Cells[i, 4] = "이름";
                                wSheet.Cells[i, 5] = "사용한연장시간";
                                wSheet.Cells[i, 6] = "로그";
                            }
                            if (log == "")
                            {
                                wSheet.Cells[(i + 1), 1] = "";
                                wSheet.Cells[(i + 1), 2] = "";
                                wSheet.Cells[(i + 1), 3] = "";
                                wSheet.Cells[(i + 1), 4] = "";
                                wSheet.Cells[(i + 1), 5] = "";
                                wSheet.Cells[(i + 1), 6] = "";
                            }
                            else if (log != "" && i != 1)
                            {
                                wSheet.Cells[(i + 1), 1] = token[0];
                                wSheet.Cells[(i + 1), 2] = token[1];
                                wSheet.Cells[(i + 1), 3] = token[2];
                                wSheet.Cells[(i + 1), 4] = token[3];
                                wSheet.Cells[(i + 1), 5] = token[4];
                                wSheet.Cells[(i + 1), 6] = token[5];
                            }
                        }
                        sr.Close();
                        wbook.SaveAs(@ExcelSaveDialog.FileName, Excel.XlFileFormat.xlWorkbookNormal, missingType, missingType, missingType, missingType, Excel.XlSaveAsAccessMode.xlNoChange, missingType, false, missingType, missingType, false);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(xls);
                        Process[] ExCel = Process.GetProcessesByName("EXCEL");
                        if (ExCel.Count() != 0)
                        {
                            ExCel[0].Kill();
                        }
                    }
                    else
                    {
                        MessageBox.Show("엑셀로 변경할 파일이 없습니다.");
                    }
                }
                catch (Exception)
                {
                    //MessageBox.Show("엑셀 파일로 변경중에 오류가 발생했습니다.");
                    Process[] ExCel = Process.GetProcessesByName("EXCEL");
                    if (ExCel.Count() != 0)
                    {
                        ExCel[0].Kill();
                    }
                }
            }

Posted by 철냄비짱
,