您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 数据通信与网络 > 在C#中如何把数据写入Excel?
在C#中如何把数据写入Excel?C#中使用ExcelusingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Reflection;usingSystem.Runtime.InteropServices;usingMicrosoft.Office.Interop.Excel;usingExcelApplication=Microsoft.Office.Interop.Excel.ApplicationClass;namespaceExcalDemo...{publicclassExcelFiles...{publicvoidCreateExcelFiles()...{//创建一个Excel实例ExcelApplicationexcel=newExcelApplication();try...{//注释掉的语句是:从磁盘指定位置打开一个Excel文件//excel.Workbooks.Open(demo.xls,Missing.Value,Missing.Value,//Missing.Value,Missing.Value,Missing.Value,Missing.Value,//Missing.Value,Missing.Value,Missing.Value,Missing.Value,//Missing.Value,Missing.Value,Missing.Value,Missing.Value);excel.Visible=false;//不显示Excel文件,如果为true则显示Excel文件excel.Workbooks.Add(Missing.Value);//添加工作簿Worksheetsheet=(Worksheet)excel.ActiveSheet;//获取当前工作表Rangerange=null;//创建一个空的单元格对象sheet.get_Range(sheet.Cells[29,2],sheet.Cells[29,2]).Orientation=Excel.XlOrientation.xlVertical;//字体竖直居中在单元格内range=sheet.get_Range(A1,Missing.Value);//获取单个单元格range.RowHeight=20;//设置行高range.ColumnWidth=20;//设置列宽range.Borders.LineStyle=1;//设置单元格边框range.Font.Bold=true;//加粗字体range.Font.Size=20;//设置字体大小range.Font.ColorIndex=5;//设置字体颜色range.Interior.ColorIndex=6;//设置单元格背景色Thecontentisfrom:()THeAdderss:=6;//设置单元格背景色range.HorizontalAlignment=XlHAlign.xlHAlignCenter;//设置单元格水平居中range.VerticalAlignment=XlVAlign.xlVAlignCenter;//设置单元格垂直居中range.Value2=设置行高和列宽;//设置单元格的值range=sheet.get_Range(B2,D4);//获取多个单元格range.Merge(Missing.Value);//合并单元格range.Columns.AutoFit();//设置列宽为自动适应range.NumberFormatLocal=#,##0.00;//设置单元格格式为货币格式//设置单元格左边框加粗range.Borders[XlBordersIndex.xlEdgeLeft].Weight=XlBorderWeight.xlThick;//设置单元格右边框加粗range.Borders[XlBordersIndex.xlEdgeRight].Weight=XlBorderWeight.xlThick;range.Value2=合并单元格;//页面设置sheet.PageSetup.PaperSize=XlPaperSize.xlPaperA4;//设置页面大小为A4sheet.PageSetup.Orientation=XlPageOrientation.xlPortrait;//设置垂直版面sheet.PageSetup.HeaderMargin=0.0;//设置页眉边距sheet.PageSetup.FooterMargin=0.0;//设置页脚边距sheet.PageSetup.LeftMargin=excel.InchesToPoints(0.354330708661417);//设置左边距sheet.PageSetup.RightMargin=excel.InchesToPoints(0.354330708661417);//设置右边距sheet.PageSetup.TopMargin=excel.InchesToPoints(0.393700787401575);//设置上边距sheet.PageSetup.BottomMargin=excel.InchesToPoints(0.393700787401575);//设置下边距sheet.PageSetup.CenterHorizontally=true;//设置水平居中//打印文件sheet.PrintOut(Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);//保存文件到程序运行目录下sheet.SaveAs(System.Windows.Forms.Application.StartupPath+\demo.xls,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);excel.ActiveWorkbook.Close(false,null,null);//关闭Excel文件且不保存}catch(Exceptionex)...{MessageBox.Show(ex.Message);}Thecontentisfrom:()THeAdderss:中使用ExcelusingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Reflection;usingSystem.Runtime.InteropServices;usingMicrosoft.Office.Interop.Excel;usingExcelApplication=Microsoft.Office.Interop.Excel.ApplicationClass;namespaceExcalDemo...{publicclassExcelFiles...{publicvoidCreateExcelFiles()...{//创建一个Excel实例ExcelApplicationexcel=newExcelApplication();try...{//注释掉的语句是:从磁盘指定位置打开一个Excel文件//excel.Workbooks.Open(demo.xls,Missing.Value,Missing.Value,//Missing.Value,Missing.Value,Missing.Value,Missing.Value,//Missing.Value,Missing.Value,Missing.Value,Missing.Value,//Missing.Value,Missing.Value,Missing.Value,Missing.Value);excel.Visible=false;//不显示Excel文件,如果为true则显示Excel文件excel.Workbooks.Add(Missing.Value);//添加工作簿Worksheetsheet=(Worksheet)excel.ActiveSheet;//获取当前工作表Rangerange=null;//创建一个空的单元格对象sheet.get_Range(sheet.Cells[29,2],sheet.Cells[29,2]).Orientation=Excel.XlOrientation.xlVertical;//字体竖直居中在单元格内range=sheet.get_Range(A1,Missing.Value);//获取单个单元格range.RowHeight=20;//设置行高range.ColumnWidth=20;//设置列宽range.Borders.LineStyle=1;//设置单元格边框range.Font.Bold=true;//加粗字体range.Font.Size=20;//设置字体大小range.Font.ColorIndex=5;//设置字体颜色range.Interior.ColorIndex=6;//设置单元格背景色range.HorizontalAlignment=XlHAlign.xlHAlignCenter;//设置单元格水平居中range.VerticalAlignment=XlVAlign.xlVAlignCenter;//设置单元格垂直居中range.Value2=设置行高和列宽;//设置单元格的值range=sheet.get_Range(B2,D4);//获取多个单元格range.Merge(Missing.Value);//合并单元格range.Columns.AutoFit();//设置列宽为自动适应range.NumberFormatLocal=#,##0.00;//设置单元格格式为货币格式//设置单元格左边框加粗range.Borders[XlBordersIndex.xlEdgeLeft].Weight=XlBorderWeight.xlThick;//设置单元格右边框加粗range.Borders[XlBordersIndex.xlEdgeRight].Weight=XlBorderWeight.xlThick;range.Value2=合并单元格;//页面设置sheet.PageSetup.PaperSize=XlPaperSize.xlPaperA4;//设置页面大小为A4sheet.PageSetup.Orientation=XlPageOrientation.xlPortrait;//设置垂直版面sheet.PageSetup.HeaderMargin=0.0;//设置页眉边距sheet.PageSetup.FooterMargin=0.0;//设置页脚边距sheet.PageSetup.LeftMargin=excel.InchesToPoints(0.354330708661417);//设置左边距sheet.PageSetup.RightMargin=excel.InchesToPoints(0.354330708661417);//设置右边距sheet.PageSetup.TopMargin=ex
本文标题:在C#中如何把数据写入Excel?
链接地址:https://www.777doc.com/doc-4694057 .html