2012-06-26 1 views
4

난 bold.Here 내 코드데이터 집합을 내보내는 동안 헤더 글꼴을 굵게 만드는 방법은 무엇입니까? 여기

   HttpContext.Current.Response.Clear(); 
       HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ""); 
       HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; 
       StringWriter stringWriter = new StringWriter(); 
       HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter); 
       DataGrid dataExportExcel = new DataGrid(); 
       foreach (DataTable table in dtInputParameters.Tables) 
       { 
        dataExportExcel.DataSource = table; 
        dataExportExcel.DataBind(); 
        dataExportExcel.RenderControl(htmlWrite); 
        htmlWrite.WriteLine("<br/>"); 
        // htmlWrite.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontWeight, "bold"); 
       } 
       StringBuilder sbResponseString = new StringBuilder(); 
       sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + worksheetName + "</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>"); 
       sbResponseString.Append(stringWriter + "<table width='800' height='100' align='center' style='text-align:center'"); 
       sbResponseString.Append("</table></body></html>"); 
       HttpContext.Current.Response.Write(sbResponseString.ToString()); 
       HttpContext.Current.Response.End(); 

어떤 제안 인 데이터 테이블 혼자 모양의 헤더 글꼴을 만들기 위해 excel.How하는 데이터 세트의 datatables을 수출하고있다?

답변

7

굵은 글꼴을 사용하려면 DataGrid의 HeaderStyle을 설정해야합니다. 그게 다야.

dataExportExcel.HeaderStyle.Font.Bold=true; 
1
Cursor.Current = Cursors.WaitCursor; 
     try 
     { 
      SaveFileDialog sfd = new SaveFileDialog(); 
      sfd.FileName = "Total Expiry Inventories Data "; 
      sfd.DefaultExt = "xls"; 
      sfd.Filter = "xlsx files(*.xlsx)|*.xlsx"; 
      if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK) 
      { 
       return; 
      } 

      Excel.Application ExcelApp = new Excel.Application(); 
      Excel.Workbook workbook = ExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); 
      Excel.Worksheet worksheet1 = (Excel.Worksheet)workbook.Worksheets[1]; 
      worksheet1.Name = "Expiry Data"; 
      for (int i = 1; i < GrdViewData.Columns.Count + 1; i++) 
      { 
       worksheet1.Cells[1, i] = GrdViewData.Columns[i - 1].HeaderText; 
       worksheet1.Cells[1, i].Font.Bold = true; 


      } 
      for (int i = 0; i < GrdViewData.Rows.Count; i++) 
      { 
       for (int j = 0; j < GrdViewData.Columns.Count; j++) 
       { 
        worksheet1.Cells[i + 2, j + 1] = GrdViewData.Rows[i].Cells[j].Value.ToString(); 
       } 
      } 
      worksheet1.Rows.Font.Size = 12; 
      // Excel.Range range_Consolidated = worksheet1.Rows.get_Range("a1", "d1"); 
      // range_Consolidated.Font.Bold = true; 

      // range_Consolidated.Font.Italic = true; 

      string ExcelFileName = sfd.FileName; 
      workbook.SaveAs(ExcelFileName); 
      workbook.Close(false, ExcelFileName, Missing.Value); 
      ExcelApp.Quit(); 

      ExcelApp = null; 
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
      MessageBox.Show("File Saved! you can open it from\n '" + sfd.FileName + "'", "EXPORT", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 

     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
     } 
관련 문제