WPF导出,07和03版本的两种方法

wpf导出,前面是实现07以后的版本 ,表格样式可根据要求调试

 if (aLastName== ".xlsx")
            {
                Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook excelWB = excelApp.Workbooks.Add(System.Type.Missing);    //创建工作簿(WorkBook:即Excel文件主体本身)  
                Microsoft.Office.Interop.Excel.Worksheet excelWS = (Microsoft.Office.Interop.Excel.Worksheet)excelWB.Worksheets[1];

                Microsoft.Office.Interop.Excel.Range merge1;
                Microsoft.Office.Interop.Excel.Range time;
                Microsoft.Office.Interop.Excel.Range title1;
                Microsoft.Office.Interop.Excel.Range title2;
                Microsoft.Office.Interop.Excel.Range title3;
                Microsoft.Office.Interop.Excel.Range rang1;

                string HN = "F" + columnCount;

                merge1 = excelWS.get_Range("A1", "F1");
                merge1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                merge1.Font.Name = "宋体";
                merge1.Font.Size = 18;
                merge1.Font.Bold = FontBoldWeight.Bold;
                merge1.RowHeight = 40;
                merge1.Merge(0);

                time = excelWS.get_Range("A2", "F2");
                time.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;
                time.Font.Name = "宋体";
                time.Font.Size = 15;
                time.RowHeight = 20;
                time.Merge(0);

                title1 = excelWS.get_Range("A3", "A3");
                title1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                title1.Font.Name = "宋体";
                title1.ColumnWidth = 10;
                title1.RowHeight = 30;
                title1.Font.Size = 13;
                title1.Font.Bold = FontBoldWeight.Bold;
                title2 = excelWS.get_Range("B3", "B3");
                title2.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                title2.Font.Name = "宋体";
                title2.ColumnWidth = 15;
                title2.RowHeight = 30;
                title2.Font.Size = 13;
                title2.Font.Bold = FontBoldWeight.Bold;
                title3 = excelWS.get_Range("C3", "F3");
                title3.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                title3.Font.Name = "宋体";
                title3.ColumnWidth = 30;
                title3.RowHeight = 30;
                title3.Font.Size = 13;
                title3.Font.Bold = FontBoldWeight.Bold;
                string RH = "F" + (rowCount + 3);
                rang1 = excelWS.get_Range("A4", RH);
                rang1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rang1.Font.Name = "宋体";
                rang1.RowHeight = 25;

                excelWS.Cells[1, 1] = Title;
                excelWS.Cells[2, 1] = date;
                excelWS.Cells[3, 1] = "序号";
                excelWS.Cells[3, 2] = "操作员";
                excelWS.Cells[3, 3] = "操作时间";
                excelWS.Cells[3, 4] = "操作内容";
                excelWS.Cells[3, 5] = "操作主机";
                excelWS.Cells[3, 6] = "登录IP";

                for (int i = 0; i < rowCount; i++)
                {
                    excelWS.Cells[i + 4, 1] = dg.Rows[i][1].ToString();  
                    excelWS.Cells[i + 4, 2] = dg.Rows[i][2].ToString();  
                    excelWS.Cells[i + 4, 3] = dg.Rows[i][3].ToString();  
                    excelWS.Cells[i + 4, 4] = dg.Rows[i][4].ToString();  
                    excelWS.Cells[i + 4, 5] = dg.Rows[i][5].ToString();  
                    excelWS.Cells[i + 4, 6] = dg.Rows[i][6].ToString();  
                }
                excelWB.SaveAs(File);
                excelWB.Close();
                excelApp.Quit();
            }
            else
            {
                HSSFWorkbook workbook = new HSSFWorkbook();//创建workbook对象
                HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
                //定义title的font和cell样式
                HSSFFont fontTitle = SheetStyle.GetFont(workbook, 18, FontBoldWeight.Bold);
                HSSFCellStyle styleTitle = SheetStyle.GetStyle(workbook);
                styleTitle.SetFont(fontTitle);
                //定义header的font和cell样式
                HSSFFont fontHeader = SheetStyle.GetFont(workbook, 13, FontBoldWeight.Bold);
                HSSFCellStyle styleHeaderT = SheetStyle.GetStyle(workbook);
                styleHeaderT.SetFont(fontHeader);
                //定义date的font和cell样式
                HSSFFont fontDate = SheetStyle.GetFont(workbook, 15);
                HSSFCellStyle styleDate = SheetStyle.GetStyle(workbook);
                styleDate.SetFont(fontDate);
                //定义content的font和cell样式
                HSSFFont fontcontent = SheetStyle.GetFont(workbook, 11);
                HSSFCellStyle styleContent = SheetStyle.GetStyle(workbook);
                styleContent.SetFont(fontcontent);

                //标题
                HSSFRow rowTitle = (HSSFRow)sheet.CreateRow(0);
                rowTitle.HeightInPoints = 40;//设置行高
                HSSFCell cellTitle = (HSSFCell)rowTitle.CreateCell(0);
                cellTitle.CellStyle = styleTitle;//设置标题样式
                cellTitle.SetCellValue(Title);//给标题位置赋值
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, columnCount - 2));//合并单元格,行为一行,列为DataGrid的列数

                HSSFRow rowDate = (HSSFRow)sheet.CreateRow(1);
                rowDate.HeightInPoints = 20;
                HSSFCell cellDate = (HSSFCell)rowDate.CreateCell(0);//位置在列数减2的格上
                cellDate.CellStyle = styleDate;
                cellDate.CellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
                sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, columnCount - 2));
                cellDate.SetCellValue(date);
               
                

                //添加表头
                HSSFRow rowHeader = (HSSFRow)sheet.CreateRow(2);
                rowHeader.HeightInPoints = 30;
                HSSFCell cellHeader1 = (HSSFCell)rowHeader.CreateCell(0);
                cellHeader1.SetCellValue("序号");
                cellHeader1.CellStyle = styleHeaderT;
                HSSFCell cellHeader2 = (HSSFCell)rowHeader.CreateCell(1);
                cellHeader2.SetCellValue("操作员");
                cellHeader2.CellStyle = styleHeaderT;
                HSSFCell cellHeader3 = (HSSFCell)rowHeader.CreateCell(2);
                cellHeader3.SetCellValue("操作时间");
                cellHeader3.CellStyle = styleHeaderT;
                HSSFCell cellHeader4 = (HSSFCell)rowHeader.CreateCell(3);
                cellHeader4.SetCellValue("操作内容");
                cellHeader4.CellStyle = styleHeaderT;
                HSSFCell cellHeader5 = (HSSFCell)rowHeader.CreateCell(4);
                cellHeader5.SetCellValue("操作主机");
                cellHeader5.CellStyle = styleHeaderT;
                HSSFCell cellHeader6 = (HSSFCell)rowHeader.CreateCell(5);
                cellHeader6.SetCellValue("登录IP");
                cellHeader6.CellStyle = styleHeaderT;

                //设置列宽
                sheet.SetColumnWidth(0, 10 * 300);
                sheet.SetColumnWidth(1, 15 * 300);
                for (int i = 2; i < 6; i++)
                {
                    sheet.SetColumnWidth(i, 30 * 300);
                }

                //添加内容
                for (int i = 0; i < rowCount; i++)
                {
                    HSSFRow rowContent = (HSSFRow)sheet.CreateRow(3+i);
                    rowContent.HeightInPoints = 30;
                    for (int j = 1; j < columnCount; j++)
                    {
                        HSSFCell cellContent = (HSSFCell)rowContent.CreateCell(j - 1);
                        cellContent.CellStyle = styleContent;
                        cellContent.SetCellValue(dg.Rows[i][j].ToString());
                    }
                }
                string outputPath = File;
                FileStream outStream = new FileStream(outputPath, FileMode.Create);
                workbook.Write(outStream);
                outStream.Close();
            }

 

你可能感兴趣的:(C#,WPF)