c# 代码创建Excel文件 并且 将数据导出Excel

  1. 导入Microsoft.Office.Interop.Excel.dll
  2. using System.IO;
  3.  #region 创建Excel
  4.     /// <summary>
  5.     /// 创建Excel
  6.     /// </summary>
  7.     /// <param name="fileName">文件名称</param>
  8.     /// <param name="sheetName">sheet名称</param>
  9.     private void CreateExcel(string fileName,string sheetName)
  10.     {
  11.         Microsoft.Office.Interop.Excel.ApplicationClass my = new Microsoft.Office.Interop.Excel.ApplicationClass();
  12.         my.Visible = true;
  13.         object objMissing = System.Reflection.Missing.Value;
  14.         //打开工作簿   
  15.         Microsoft.Office.Interop.Excel.Workbook mybook = (Microsoft.Office.Interop.Excel.Workbook)my.Workbooks.Add(1);   //   1表示只建一个表   
  16.         //mybook.Worksheets.Add(objMissing,objMissing,1,objMissing);//添加sheet   
  17.         if (sheetName == "")
  18.         {
  19.             sheetName = "steet1";
  20.         }
  21.         else
  22.         {
  23.             ((Microsoft.Office.Interop.Excel.Worksheet)mybook.Worksheets[1]).Name = sheetName;//将sheet1的名称改为zhu   
  24.         } 
  25.         Microsoft.Office.Interop.Excel.Worksheet mysheet = (Microsoft.Office.Interop.Excel.Worksheet)mybook.Worksheets[1];
  26.         ((Microsoft.Office.Interop.Excel.Range)mysheet.Cells[2, 3]).EntireRow.Insert(0,0);     //添加行   
  27.         //保存   
  28.         mybook.SaveAs("d://" + fileName + ".xls", objMissing, objMissing, objMissing,   //Excel.XlSaveAsAccessMode.xlShared   
  29.         objMissing, objMissing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared,
  30.         objMissing, objMissing, objMissing,
  31.         objMissing, objMissing);
  32.         mybook = null;
  33.         my.Quit();
  34.         my = null;
  35.     }
  36.     #endregion
  37. //------------------------------------------------------------------
  38.  #region 导入Excel
  39.     /// <summary>
  40.     /// 导入Excel
  41.     /// </summary>
  42.     /// <param name="dv">数据源</param>
  43.     /// <param name="path">路径</param>
  44.     /// <param name="title1">标题</param>
  45.     /// <param name="heji">合计</param>
  46.     public void inputExcel(DataSet dv, string path, string title1, string heji)
  47.     {
  48.         try
  49.         {
  50.             FileInfo fi = new FileInfo(path);
  51.             fi.Delete();
  52.         }
  53.         catch
  54.         {
  55.         }
  56.         Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
  57.         object oMissing = System.Reflection.Missing.Value;
  58.         Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Add(oMissing);
  59.         int rowIndex = 3;
  60.         int colIndex = 0;
  61.         excel.Cells[1, 1] = title1;
  62.         foreach (DataColumn dc in dv.Tables[0].Columns)
  63.         {
  64.             colIndex++;
  65.             excel.Cells[rowIndex, colIndex] = dc.ColumnName;
  66.             ((Microsoft.Office.Interop.Excel.Range)excel.Cells[rowIndex, colIndex]).ColumnWidth = 18.8;
  67.         }
  68.         foreach (DataRow row in dv.Tables[0].Rows)
  69.         {
  70.             rowIndex++;
  71.             colIndex = 0;
  72.             foreach (DataColumn dc in dv.Tables[0].Columns)
  73.             {
  74.                 colIndex++;
  75.                 excel.Cells[rowIndex, colIndex] = row[dc.ColumnName].ToString();
  76.                 ((Microsoft.Office.Interop.Excel.Range)excel.Cells[rowIndex, colIndex]).ColumnWidth = 18.8;
  77.             }
  78.         }
  79.         excel.Cells[rowIndex + 1, colIndex] = heji;
  80.         Microsoft.Office.Interop.Excel.Sheets sheets = book.Worksheets;
  81.         Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(1);
  82.         Microsoft.Office.Interop.Excel.Range range;
  83.         range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, colIndex]);
  84.         range.MergeCells = true;//合并   
  85.         range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中   
  86.         range.Font.Bold = true;
  87.         range.Font.Size = 14;
  88.         book.Saved = true;
  89.         excel.UserControl = false;
  90.         worksheet.SaveAs(path, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
  91.         excel.Quit();
  92.         oMissing = null;
  93.         sheets = null;
  94.         worksheet = null;
  95.         book = null;
  96.         excel = null;
  97.         GC.Collect();
  98.     }
  99.     #endregion
  100. //---------------------------------------------------------------
  101.     #region 绑定数据
  102.     private DataSet bangding()
  103.     {
  104.         using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=pubs"))
  105.         {
  106.             conn.Open();
  107.             string sql = "select job_id as 编号,job_desc as 排序,min_lvl as 最小, max_lvl as 最大 from jobs";
  108.             SqlDataAdapter dar = new SqlDataAdapter(sql, conn);
  109.             DataSet ds = new DataSet();
  110.             dar.Fill(ds);
  111.             this.GridView1.DataSource = ds;
  112.             this.GridView1.DataBind();
  113.             return ds;
  114.         }
  115.     }
  116.     #endregion
  117. //------------------------------------------------------------------
  118. #region 获取合计
  119.     private int num()
  120.     {
  121.         int num = 0;
  122.         for (int i = 0; i < bangding().Tables[0].Rows.Count; i++)
  123.         {
  124.             num += Convert.ToInt32(bangding().Tables[0].Rows[i][3].ToString());
  125.         }
  126.         return num;
  127.     }
  128.     #endregion
  129. //-------------------------------------------------------------------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
你可以使用C#中的NPOI库来创建和写入Excel文件。下面是一个简单的示例代码,演示了如何使用NPOI库创建一个Excel文件,并将数据写入到文件中: ```csharp using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.SS.Util; using System.IO; public void ExportToExcel(string filePath) { // 创建一个Workbook对象,表示整个Excel文件 IWorkbook workbook = new HSSFWorkbook(); // 创建一个Sheet对象,表示Excel中的一个Sheet ISheet sheet = workbook.CreateSheet("Sheet1"); // 创建第一行,并写入表头数据 IRow headerRow = sheet.CreateRow(0); headerRow.CreateCell(0).SetCellValue("姓名"); headerRow.CreateCell(1).SetCellValue("年龄"); headerRow.CreateCell(2).SetCellValue("性别"); // 写入数据行 for (int i = 0; i < data.Count; i++) { IRow dataRow = sheet.CreateRow(i + 1); dataRow.CreateCell(0).SetCellValue(data[i].Name); dataRow.CreateCell(1).SetCellValue(data[i].Age); dataRow.CreateCell(2).SetCellValue(data[i].Gender); } // 调整列宽 sheet.AutoSizeColumn(0); sheet.AutoSizeColumn(1); sheet.AutoSizeColumn(2); // 将Workbook保存为Excel文件 using (FileStream fs = new FileStream(filePath, FileMode.Create)) { workbook.Write(fs); } } ``` 以上代码创建了一个名为"Sheet1"的工作表,并在第一行写入了表头数据。然后,使用循环将数据写入到数据行中。最后,将Workbook保存为Excel文件。 在使用该代码之前,你需要在项目中引用NPOI库,并在代码中引入相应的命名空间。此外,你可能需要根据自己的需求修改代码以适应你的数据结构和文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值