Aspose.Cells 구성 요 소 를 사용 하여 Excel 파일 인 스 턴 스 생 성

헤더 가 있 는 Excel 파일 을 만 듭 니 다.형식 은 다음 과 같 습 니 다.

물론 더 복잡 한 것 은 셀 을 통합 하 는 방법 인 Public void Merge(int firstRow,int firstColumn,int totalRows,int totalColumns)를 통 해 이 루어 질 수 있다.
구현 방식:
1.우선"Aspose.Cells.dll"에 대한 인용 을 추가 해 야 합 니 다.
2.실현 코드 는 다음 과 같다.

//
            Workbook workbook = new Workbook(); //
            Worksheet sheet = workbook.Worksheets[0]; //
            Cells cells = sheet.Cells;//


            Style style = workbook.Styles[workbook.Styles.Add()];//

            #region
            //
            style.HorizontalAlignment = TextAlignmentType.Center;//  
            style.Font.Name = " ";//
            style.Font.Size = 18;//  
            style.Font.IsBold = true;//

            cells.Merge(0, 0, 1, 12);               //
            cells[0, 0].PutValue(" ");   //
            cells[0, 0].SetStyle(style);            //  
            cells.SetRowHeight(0, 28);              //  


            //
            style.HorizontalAlignment = TextAlignmentType.Left;
            style.Font.Size = 11;
            style.Font.IsBold = false;
            cells.Merge(1, 0, 1, 7);
            cells[1, 0].PutValue(String.Format(" :{0} {1}",DateTime.Now.AddDays(-1).ToString("yyyy MM dd "),DateTime.Now.ToString("yyyy MM dd ")));
            cells[1, 0].SetStyle(style);
            cells.SetRowHeight(1, 20);

            //
            style.HorizontalAlignment = TextAlignmentType.Right;
            style.Font.Size = 11;
            style.Font.IsBold = false;
            cells.Merge(1, 7, 1, 5);
            cells[1, 7].PutValue(String.Format(" :{0}", DateTime.Now.ToString("yyyy MM dd ")));
            cells[1, 7].SetStyle(style);
            cells.SetRowHeight(1, 20);
            #endregion

            #region

            #region
            //
            style.HorizontalAlignment = TextAlignmentType.Center;
            cells[2, 0].PutValue(" ");
            cells[2, 0].SetStyle(style);
            cells.SetRowHeight(2, 20);
            cells.SetColumnWidthPixel(0, 38);

            //
            cells[2, 1].PutValue(" ");
            cells[2, 1].SetStyle(style);
            cells.SetColumnWidthPixel(1, 77);

            //
            cells[2, 2].PutValue(" ");
            cells[2, 2].SetStyle(style);
            cells.SetColumnWidthPixel(2, 107);

            //
            cells[2, 3].PutValue(" ");
            cells[2, 3].SetStyle(style);
            cells.SetColumnWidthPixel(3, 69);

            //   
            cells[2, 4].PutValue("    ");
            cells[2, 4].SetStyle(style);
            cells.SetColumnWidthPixel(4, 71);

            //
            cells[2, 5].PutValue(" ");
            cells[2, 5].SetStyle(style);
            cells.SetColumnWidthPixel(5, 71);

            //
            cells[2, 6].PutValue(" ");
            cells[2, 6].SetStyle(style);
            cells.SetColumnWidthPixel(6, 114);

            // 、
            cells[2, 7].PutValue(" 、 ");
            cells[2, 7].SetStyle(style);
            cells.SetColumnWidthPixel(7, 104);

            //
            cells[2, 8].PutValue(" ");
            cells[2, 8].SetStyle(style);
            cells.SetColumnWidthPixel(8, 255);

            //
            cells[2, 9].PutValue(" ");
            cells[2, 9].SetStyle(style);
            cells.SetColumnWidthPixel(9, 72);

            //
            cells[2, 10].PutValue(" ");
            cells[2, 10].SetStyle(style);
            cells.SetColumnWidthPixel(10, 72);

            //
            cells[2, 11].PutValue(" ");
            cells[2, 11].SetStyle(style);
            cells.SetColumnWidthPixel(11, 255);

            #endregion

            #endregion


            System.IO.MemoryStream ms = workbook.SaveToStream();//
            byte[] bt = ms.ToArray();

            workbook.Save(@"E:\test.xls");//
        }

3.생 성 된 Excel 은 디스크 에 저장 할 수도 있 고 웹 페이지 에서 흐 르 는 방식 으로 다운로드 할 수도 있 습 니 다.

//
            System.IO.MemoryStream ms = workbook.SaveToStream();//
            byte[] bt = ms.ToArray();

            string fileName = " " + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";//
            //   

            Response.ContentType = "application/vnd.ms-excel";

            //
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bt);

            Response.Flush();
            Response.End();

좋은 웹페이지 즐겨찾기