Пример #1
0
        /// <summary>
        /// 将数据导入到Excel
        /// </summary>
        public static void ExportExcel(string excelPath, IList <Hashtable> list, string sheetName, string[] strValue)
        {
            Workbook wb = new Workbook();

            wb.Worksheets.Add(sheetName);

            WindowOptions options = wb.WindowOptions;
            Worksheet     ws      = options.SelectedWorksheet;

            //设置标题的格式

            IWorksheetCellFormat cellFormatHead = wb.CreateNewWorksheetCellFormat();

            cellFormatHead.Alignment         = HorizontalCellAlignment.Center;
            cellFormatHead.BottomBorderStyle = CellBorderLineStyle.Thin;
            cellFormatHead.LeftBorderStyle   = CellBorderLineStyle.Thin;
            cellFormatHead.RightBorderStyle  = CellBorderLineStyle.Thin;
            cellFormatHead.TopBorderStyle    = CellBorderLineStyle.Thin;
            cellFormatHead.Font.Bold         = ExcelDefaultableBoolean.True;
            cellFormatHead.Font.Name         = "Calibri";
            cellFormatHead.Font.Height       = 220;
            ws.Rows[0].CellFormat.SetFormatting(cellFormatHead);

            //设置数据行的格式
            IWorksheetCellFormat cellFormatContent = wb.CreateNewWorksheetCellFormat();

            cellFormatContent.Alignment         = HorizontalCellAlignment.Left;
            cellFormatContent.BottomBorderStyle = CellBorderLineStyle.Thin;
            cellFormatContent.LeftBorderStyle   = CellBorderLineStyle.Thin;
            cellFormatContent.RightBorderStyle  = CellBorderLineStyle.Thin;
            cellFormatContent.TopBorderStyle    = CellBorderLineStyle.Thin;
            cellFormatContent.Font.Name         = "Calibri";
            cellFormatContent.Font.Height       = 220;

            //添加标题行

            //Hashtable headHashTable = list[0];
            int i = 0;

            foreach (string headValue in strValue)
            {
                ws.Rows[0].Cells[i].Value = headValue;
                ws.Columns[i].Width       = 5000;
                i++;
            }

            //添加数据行

            for (int k = 0; k < list.Count; k++)
            {
                Hashtable dataHashTable = list[k];
                int       j             = 0;
                foreach (string dataValue in strValue)
                {
                    ws.Rows[k + 1].Cells[j].Value = dataHashTable[dataValue];
                    ws.Rows[k + 1].Cells[j].CellFormat.SetFormatting(cellFormatContent);
                    j++;
                }
            }

            wb.Save(excelPath);
        }