.NET에서 Excel 생성

.NET에서 Excel 생성하는 좋은 방법이 무엇이 있을까?

가장 먼저 떠오르는 것은 HTML 식으로 생성하는 방법이다.

두 번째로 XML을 사용하여 생성하는 방법이다.

세 번째로는 미리 Excel 파일을 생성한 후 OleDB를 사용하여 로드하여 쓰는 것이다.

첫 번째, 두 번째 방법은 호환성 문제가 있다.

아무래도 바이너리 파일이 아니다 보니 제대로 인지 못하는 경우가 있다.

세 번째 방법은 시스템을 타는 현상이 나타났다.

개발서버에서는 정상으로 동작하는 실 서버에서는 제대로 동작을 하지 않는 문제가 있었다.

실 서버가 해외의 경우에는 문제 해결에 시간이 많이 소요된다.

이번에 찾은 다른 방법은 MyXLS 라이브러리를 사용하는 것이다.

MyXLS

사이트: http://sourceforge.net/projects/myxls/

Java에서 Excel 만드는 것과 비슷하게 프로그래밍 방식으로 Excel 파일을 만든다.

이것에 단점은 Excel 로드에 문제가 있다는 것이다.

여러 가지 기능이 지원되지만 간단한 쓰기만 테스트해 보았다.

다음은 사용 예이다.

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="org.in2bits.MyXls" %>
<script runat="server">
void Page_Load(object sender, EventArgs e) {
    try {
        XlsDocument xls = new XlsDocument();
        xls.FileName = "story_" + DateTime.Now.ToString("yyyy-MM-dd_HH_mm") + ".xls";
        xls.SummaryInformation.CodePage = 2;
        xls.SummaryInformation.Author = "Barney Kim";
        xls.SummaryInformation.Title = "Excel Write Test";
        xls.SummaryInformation.Subject = "Test";
        xls.SummaryInformation.Comments = ""; // To remove default value
        xls.DocumentSummaryInformation.Company = "barney.PE.kr";
        int sheetCount = 0;
        int rowCount = 1;
        int colCount = 1;
        Worksheet sheet = null;
        for(int i=0; i < 70000; i++) {
            Cells cells = null;
            if (rowCount == 1) {
                if (sheetCount == 1) {
                    sheet = xls.Workbook.Worksheets.AddNamed("Shee");
                } else {
                    sheet = xls.Workbook.Worksheets.AddNamed("Sheet" + sheetCount);
                }
                cells = sheet.Cells;
                cells.Merge(rowCount, rowCount, colCount, 7);
                Cell c = cells.Add(rowCount, colCount++, "My Excel Write Test");
                c.Font.Bold = true;
                c.Font.Height = 20 * 12;
                rowCount++;
                colCount = 1;
                //
                cells.Add(rowCount, colCount++, "No");
                cells.Add(rowCount, colCount++, "Name");
                cells.Add(rowCount, colCount++, "Email");
                cells.Add(rowCount, colCount++, "Title");
                cells.Add(rowCount, colCount++, "Date");
                cells.Add(rowCount, colCount++, "View Count");
                cells.Add(rowCount, colCount++, "IP Address");
                colCount = 1;
                rowCount++;
            } else {
                cells = sheet.Cells;
            }
            //
            cells.Add(rowCount, colCount++, i);
            cells.Add(rowCount, colCount++, "테스터");
            cells.Add(rowCount, colCount++, "some@some.com");
            cells.Add(rowCount, colCount++, "테스트라네");
            cells.Add(rowCount, colCount++, DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
            cells.Add(rowCount, colCount++, 2);
            cells.Add(rowCount, colCount++, "127.0.0.1");
            rowCount++;
            colCount = 1;
            if (rowCount > 50003) {
                rowCount = 1;
                sheetCount++;
            }
        }
        xls.Send();
    } catch (Exception ex) {
        Response.Write("Error occurred");
    }
}
</script>