C# Excel 설정 셀 스타일 내보내기

3320 단어 기술
C# 작업 excel
// Excel
        private void ExportExcel(string fileName, System.Data.DataTable myDGV, string title)
        {
            string saveFileName = "";
            System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
            saveDialog.DefaultExt = "xlsx";
            saveDialog.Filter = "Excel |*.xlsx";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return; // 
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                System.Windows.MessageBox.Show(" Excel , Excel");
                return;
            }
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];// sheet1

            // 
            Range rangeLecture = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, myDGV.Columns.Count]];
            rangeLecture.Application.DisplayAlerts = false;
            rangeLecture.Merge(Missing.Value);
            rangeLecture.Application.DisplayAlerts = true;
            worksheet.Cells[1, 1] = title;
           

            // 
            for (int i = 0; i < myDGV.Columns.Count; i++)
            {
                worksheet.Cells[2, i + 1] = myDGV.Columns[i].ColumnName;
            }
            // 
            for (int r = 0; r < myDGV.Rows.Count; r++)
            {
                for (int i = 0; i < myDGV.Columns.Count; i++)
                {
                    worksheet.Cells[r + 3, i + 1] = myDGV.Rows[r][i].ToString();
                }
                System.Windows.Forms.Application.DoEvents();
            }
            worksheet.Columns.EntireColumn.AutoFit();// 
            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(" , !
" + ex.Message); } } xlApp.Quit(); GC.Collect();// //System.Windows.Forms.MessageBox.Show(" ", " ", MessageBoxButtons.OK, MessageBoxIcon.Information); }

셀 스타일 작업
worksheet.Cells[1, 1].Value2 = strTitle;//셀 내 텍스트 설정하기
worksheet.Cells[1, 1].Font.Name = "송체";//글꼴 설정
worksheet.Cells[1, 1].Font.Size = 18;//글꼴 크기
worksheet.Cells[1, 1].Font.Bold = true;//굵게 표시
worksheet.Cells[1, 1].HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//수평
worksheet.Cells[1, 1].VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//수직 가운데
worksheet.Cells[1, 1].Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//테두리 설정
worksheet.Cells[1, 1].Borders.Weight = Excel.XlBorderWeight.xlMedium;//테두리 일반 굵기

좋은 웹페이지 즐겨찾기