c# 将数据导出到EXCEL文件(一)

       本文讲的是引用微软的office com组件,将数据导出到excel文件。因此使用前提是:电脑上已经安装可以使用的微软的office软件。

第一步:项目中加入微软的office的com组件引用。

在鼠标右击项目,点击【添加】弹出菜单列表,选择【项目引用】弹出【引用管理器】对话框,选择【COM】-【Microsoft Excel 16.0 Object Library】,如图所示:

第二步,编辑相关代码

文件头加 :using MSExcel = Microsoft.Office.Interop.Excel;

打开excel文件:

excelApp = new MSExcel.Application();
excelApp.Visible = false;// 设置EXCEl打开后是否可见。true:可见;false:不可见

MSExcel.Workbooks wbs = excelApp.Workbooks;

MSExcel.Workbook wb = wbs.Add(Missing.Value);// 打开临时文件

导入数据:

MSExcel.Worksheet excelSheet = workbook.Sheets[1];//定义一个新的工作表,获取excel 文件中的第一张工作表

excelSheet.Activate();// 激活工作表

excelSheet.Cells[1, 1] = "学生ID";
excelSheet.Cells[1, 2] = "姓名";
excelSheet.Cells[1, 3] = "性别";
excelSheet.Cells[1, 4] = "年龄";
excelSheet.Cells[1, 5] = "年级";
excelSheet.Cells[1, 6] = "总分";

int index = 1;
foreach (var item in studentList)
{
    int rowIndex = ++index;

    excelSheet.Cells[rowIndex, 1] = item.Id;// ID
    excelSheet.Cells[rowIndex, 2] = item.Name;
    excelSheet.Cells[rowIndex, 3] = item.Sex;
    excelSheet.Cells[rowIndex, 4] = item.Age;
    excelSheet.Cells[rowIndex, 5] = item.Class;
    excelSheet.Cells[rowIndex, 6] = item.Score;

}

保存到指定的excell文件:

wb.SaveAs(excelFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

wb.Close();// 关闭文档
wbs.Close();// 关闭工作薄
excelApp.Quit(); // 关闭excel应用程序
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

/// <summary> /// 导出Excel /// </summary> /// <param name="table"></param> /// <returns></returns> public bool ToExcel(DataTable table) { FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); IWorkbook workBook = new HSSFWorkbook(); this._sheetName = this._sheetName.IsEmpty() ? "sheet1" : this._sheetName; ISheet sheet = workBook.CreateSheet(this._sheetName); //处理表格标题 IRow row = sheet.CreateRow(0); row.CreateCell(0).SetCellValue(this._title); sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1)); row.Height = 500; ICellStyle cellStyle = workBook.CreateCellStyle(); IFont font = workBook.CreateFont(); font.FontName = "微软雅黑"; font.FontHeightInPoints = 17; cellStyle.SetFont(font); cellStyle.VerticalAlignment = VerticalAlignment.Center; cellStyle.Alignment = HorizontalAlignment.Center; row.Cells[0].CellStyle = cellStyle; //处理表格列头 row = sheet.CreateRow(1); for (int i = 0; i < table.Columns.Count; i++) { row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName); row.Height = 350; sheet.AutoSizeColumn(i); } //处理数据内容 for (int i = 0; i < table.Rows.Count; i++) { row = sheet.CreateRow(2 + i); row.Height = 250; for (int j = 0; j < table.Columns.Count; j++) { row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString()); sheet.SetColumnWidth(j, 256 * 15); } } //写入数据流 workBook.Write(fs); fs.Flush(); fs.Close(); return true; } /// <summary> /// 导出Excel /// </summary> /// <param name="table"></param> /// <param name="title"></param> /// <param name="sheetName"></param> /// <returns></returns> public bool ToExcel(DataTable table, string title, string sheetName, string filePath) { this._title = title; this._sheetName = sheetName; this._filePath = filePath; return ToExcel(table); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值