c# excel xlsx 保存

   public XSSFWorkbook Excel_Export(DataTable query,string title,int[] rowweight,string[] rowtitle)
        {

            XSSFWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("Sheet1");
            IRow row = sheet.CreateRow(0);
            int ii = 0;
             

            ICellStyle style = workbook.CreateCellStyle();
            style.Alignment = HorizontalAlignment.Center;
            style.WrapText = true;
            style.BorderLeft = BorderStyle.Thin;
            style.BorderRight = BorderStyle.Thin;
            style.BorderTop = BorderStyle.Thin;
            style.BorderBottom = BorderStyle.Thin;
            style.VerticalAlignment = VerticalAlignment.Center;

            //设置背景颜色...
            style.FillForegroundColor = 0;
            style.FillPattern = FillPattern.SolidForeground;
            ((XSSFColor)style.FillForegroundColorColor).SetRgb(new byte[] { 215, 228, 188 });


            IFont font = workbook.CreateFont();
            font.FontHeightInPoints = 11;
            font.FontName = "微软雅黑";
            font.IsBold = true;
            style.SetFont(font);

            ICell cell = row.CreateCell(ii);

            if(!string.IsNullOrEmpty(title))
            {
                row.Height = 50 * 20;
                cell.SetCellValue(title);
                cell.CellStyle = style;
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, query.Columns.Count-1));
                ii += 1;
            }

            style = workbook.CreateCellStyle();
            style.Alignment = HorizontalAlignment.Left;
            style.WrapText = true;

            style.VerticalAlignment = VerticalAlignment.Center;
            style.BorderLeft = BorderStyle.Thin;
            style.BorderRight = BorderStyle.Thin;
            style.BorderTop = BorderStyle.Thin;
            style.BorderBottom = BorderStyle.Thin;


            //设置背景颜色...
            style.FillForegroundColor = 0;
            style.FillPattern = FillPattern.SolidForeground;
            ((XSSFColor)style.FillForegroundColorColor).SetRgb(new byte[] { 215, 228, 188 });


            font = workbook.CreateFont();
            font.FontHeightInPoints = 11;
            font.FontName = "微软雅黑";
            style.SetFont(font);


            IRow row1 = sheet.CreateRow(ii);
            row1.Height = 60 * 20;

            int i = 0;
            if(rowtitle.Length>0)
                foreach (var item in rowtitle)
                {
                    cell = row1.CreateCell(i);
                    cell.SetCellValue(item.ToString());
                    cell.CellStyle = style;
                    i += 1;
                }
            else
                foreach (var item in  query.Columns)
                {
                    cell = row1.CreateCell(i);
                    cell.SetCellValue(item.ToString());
                    cell.CellStyle = style;
                    i += 1;
                }
      
            for ( i = 0; i < rowweight.Length; i++)
            {
                if (rowweight[i] > 0)
                {
                    row1.Cells[i].CellStyle = style; //把样式赋给单元格
                    sheet.SetColumnWidth(i, rowweight[i] * 256);//设置列宽
                }
            }
            i = 0;
            row.Height = 30 * 20;
            style = workbook.CreateCellStyle();
            style.WrapText = true;
            style.Alignment = HorizontalAlignment.Left;
            style.VerticalAlignment = VerticalAlignment.Center;
            style.BorderLeft = BorderStyle.Thin;
            style.BorderRight = BorderStyle.Thin;
            style.BorderTop = BorderStyle.Thin;
            style.BorderBottom = BorderStyle.Thin;
            font = workbook.CreateFont();
            font.FontHeightInPoints = 10;
            font.FontName = "微软雅黑";
            style.SetFont(font);
            for (int n = 0; n < query.Rows.Count; n++)
            {
                ii += 1;
                IRow rowtemp = sheet.CreateRow(ii);
                for (int j = 0; j < query.Columns.Count; j++)
                {
                    cell = rowtemp.CreateCell(j);
                    cell.CellStyle = style;
                    cell.SetCellValue(query.Rows[n][j]?.ToString() ?? "");  
              
                 }
            }
            return workbook;
        }
        int[] weight = new int[] { 30,50,50,25};
            XSSFWorkbook workbook = Excel_Export(query, "", weight, new string[0]);

            ISheet sheet = workbook.GetSheet("Sheet1");


string fileName = string.Concat(string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now), ".xlsx"); Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("Content-Disposition", fileName); //写入到客户端 System.IO.MemoryStream ms = new System.IO.MemoryStream(); workbook.Write(ms); Response.BinaryWrite(ms.ToArray()); Response.Flush(); Response.End();

猜你喜欢

转载自www.cnblogs.com/LiuFengH/p/10578434.html