NPOI之C#下載Excel

Java中這個類庫叫POI,C#中叫NPOI,不少從Java一直到.Net平臺的類庫爲了區別大部分都是在前面加個N,好比Hibernate和NHibernate。app

npoi下載地址字體

1、使用NPOI下載大體分如下步驟:spa

  一、建立workbookcode

  二、建立sheet對象

  三、建立row和cellblog

  四、填充數據get

  五、設置cell的樣式和字體數學

2、HSSFWorkbook和XSSFWorkbook 二者用法基本同樣string

  HSSFWorkbook 建立的是Excel2003it

    HSSFSheet、HSSFCellStyle、HSSFFont等

  XSSFWorkbook 建立的是Excel2007

    XSSFSheet、XSSFCellStyle、XSSFFont等

3、行或列合併

   sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 0)); //起始行  結束行 起始列 結束列

   sheet.GetRow(0).GetCell(0).SetCellValue("姓名");//賦值

   賦值時必須使用合併後左上角單元格的行列座標

4、代碼(以Excel2003爲例):

using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;

public ActionResult Index()
        {

            List<string> headList = new List<string>();
            headList.Add("語文");
            headList.Add("數學");
            headList.Add("英語");
            headList.Add("政治");

            List<string> nameList = new List<string>();
            nameList.Add("張三");
            nameList.Add("李四");
            nameList.Add("王五");
            nameList.Add("趙六");
            nameList.Add("勝七");
            nameList.Add("朱重八");
            nameList.Add("九喇嘛");

            MemoryStream memoryStream = new MemoryStream();
            HSSFWorkbook workbook = new HSSFWorkbook();

            HSSFSheet sheet = workbook.CreateSheet();
            //建立單元格設置對象
            HSSFCellStyle cellStyle = workbook.CreateCellStyle();
            //設置水平、垂直居中
            cellStyle.Alignment = HSSFCellStyle.ALIGN_CENTER;
            cellStyle.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;
            //單元格填充顏色
            cellStyle.FillForegroundColor = HSSFColor.PALE_BLUE.index;
            cellStyle.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
            //設置邊框
            cellStyle.BorderBottom = HSSFCellStyle.BORDER_THIN;
            cellStyle.BorderLeft = HSSFCellStyle.BORDER_THIN;
            cellStyle.BorderRight = HSSFCellStyle.BORDER_THIN;
            cellStyle.BorderTop = HSSFCellStyle.BORDER_THIN;

            //建立設置字體對象
            HSSFFont font = workbook.CreateFont();
            font.FontHeightInPoints = 16;//設置字體大小
            font.Boldweight = short.MaxValue; //加粗
            font.FontName = "宋體";
            cellStyle.SetFont(font);


            //建立Excel行和單元格
            for (int i = 0; i < nameList.Count + 2; i++)
            {
                HSSFRow newRow = sheet.CreateRow(i);
                for (int j = 0; j <headList.Count+1; j++)
                {
                    newRow.CreateCell(j);
                }
            }

            //設置Excel表頭
            sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 0)); //起始行  結束行 起始列 結束列
            sheet.GetRow(0).GetCell(0).CellStyle = cellStyle;
            sheet.GetRow(1).GetCell(0).CellStyle = cellStyle;
            sheet.GetRow(0).GetCell(0).SetCellValue("姓名");//姓名

            sheet.AddMergedRegion(new CellRangeAddress(0, 0, 1, 4)); //起始行  結束行 起始列 結束列
            sheet.GetRow(0).GetCell(1).CellStyle = cellStyle;
            sheet.GetRow(0).GetCell(4).CellStyle = cellStyle;
            sheet.GetRow(0).GetCell(1).SetCellValue("課程");//課程

            for (int i = 0; i < headList.Count; i++)
            {
                sheet.GetRow(1).GetCell(i + 1).CellStyle = cellStyle;
                sheet.GetRow(1).GetCell(i + 1).SetCellValue(headList[i]);//具體課程
            }

            //導入數據
            for (int i = 0; i < nameList.Count; i++)
            {
                sheet.GetRow(i + 2).GetCell(0).SetCellValue(nameList[i]);
                for (int j = 0; j < 4; j++)
                {
                     sheet.GetRow(i + 2).GetCell(j+1).SetCellValue(i*j);
                }
            }

            workbook.Write(memoryStream);
            memoryStream.Seek(0,SeekOrigin.Begin);
          
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode("test.xls"));
            Response.ContentType = "application/octet-stream";
            Response.Charset = "gb2312";
            Response.ContentEncoding = Encoding.UTF8;
            Response.BinaryWrite(memoryStream.GetBuffer());
            Response.Flush();
            Response.End();
            return new EmptyResult();
        }

 最後結果:

 

轉載請註明出處!

相關文章
相關標籤/搜索