C#导出Excel的示例详解
作者:YellowCool 时间:2021-12-03 01:55:51
本文实例为大家分享了C#导出Excel的具体代码,供大家参考,具体内容如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Reflection;
namespace DMS
{
/// <summary>
/// C#操作Excel类
/// </summary>
class ExcelOperate
{
//法一
//public bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
//{
// DataTable dataTable = dataSet.Tables[0];
// int rowNumber = dataTable.Rows.Count;
// int columnNumber = dataTable.Columns.Count;
// if (rowNumber == 0)
// {
// MessageBox.Show("没有任何数据可以导入到Excel文件!");
// return false;
// }
// //建立Excel对象
// Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
// excel.Application.Workbooks.Add(true);
// excel.Visible = isShowExcle;//是否打开该Excel文件
// //填充数据
// for (int c = 0; c < rowNumber; c++)
// {
// for (int j = 0; j < columnNumber; j++)
// {
// excel.Cells[c + 1, j + 1] = dataTable.Rows[c].ItemArray[j];
// }
// }
// return true;
//}
//法二
//public bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
//{
// DataTable dataTable = dataSet.Tables[0];
// int rowNumber = dataTable.Rows.Count;
// int rowIndex = 1;
// int colIndex = 0;
// if (rowNumber == 0)
// {
// return false;
// }
// //建立Excel对象
// Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
// excel.Application.Workbooks.Add(true);
// excel.Visible = isShowExcle;
// //生成字段名称
// foreach (DataColumn col in dataTable.Columns)
// {
// colIndex++;
// excel.Cells[1, colIndex] = col.ColumnName;
// }
// //填充数据
// foreach (DataRow row in dataTable.Rows)
// {
// rowIndex++;
// colIndex = 0;
// foreach (DataColumn col in dataTable.Columns)
// {
// colIndex++;
// excel.Cells[rowIndex, colIndex] = row[col.ColumnName];
// }
// }
// return true;
//}
//法三(速度最快)
/// <summary>
/// 将数据集中的数据导出到EXCEL文件
/// </summary>
/// <param name="dataSet">输入数据集</param>
/// <param name="isShowExcle">是否显示该EXCEL文件</param>
/// <returns></returns>
public bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
{
DataTable dataTable = dataSet.Tables[0];
int rowNumber = dataTable.Rows.Count;//不包括字段名
int columnNumber = dataTable.Columns.Count;
int colIndex = 0;
if (rowNumber == 0)
{
return false;
}
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//excel.Application.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
excel.Visible = isShowExcle;
//Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range;
//生成字段名称
foreach (DataColumn col in dataTable.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
object[,] objData = new object[rowNumber, columnNumber];
for (int r = 0; r < rowNumber; r++)
{
for (int c = 0; c < columnNumber; c++)
{
objData[r, c] = dataTable.Rows[r][c];
}
//Application.DoEvents();
}
// 写入Excel
range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]);
//range.NumberFormat = "@";//设置单元格为文本格式
range.Value2 = objData;
worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, 1]).NumberFormat = "yyyy-m-d h:mm";
return true;
}
//法四
//public bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
//{
// DataTable dataTable = dataSet.Tables[0];
// int rowNumber = dataTable.Rows.Count;
// int columnNumber = dataTable.Columns.Count;
// String stringBuffer = "";
// if (rowNumber == 0)
// {
// MessageBox.Show("没有任何数据可以导入到Excel文件!");
// return false;
// }
// //建立Excel对象
// Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
// excel.Application.Workbooks.Add(true);
// excel.Visible = isShowExcle;//是否打开该Excel文件
// //填充数据
// for (int i = 0; i < rowNumber; i++)
// {
// for (int j = 0; j < columnNumber; j++)
// {
// stringBuffer += dataTable.Rows[i].ItemArray[j].ToString();
// if (j < columnNumber - 1)
// {
// stringBuffer += "\t";
// }
// }
// stringBuffer += "\n";
// }
// Clipboard.Clear();
// Clipboard.SetDataObject(stringBuffer);
// ((Microsoft.Office.Interop.Excel.Range)excel.Cells[1, 1]).Select();
// ((Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveWorkbook.ActiveSheet).Paste(Missing.Value, Missing.Value);
// Clipboard.Clear();
// return true;
//}
//public bool DataSetToExcel(DataSet dataSet, string fileName, bool isShowExcle)
//{
// DataTable dataTable = dataSet.Tables[0];
// int rowNumber = dataTable.Rows.Count;
// int columnNumber = dataTable.Columns.Count;
// if (rowNumber == 0)
// {
// MessageBox.Show("没有任何数据可以导入到Excel文件!");
// return false;
// }
// //建立Excel对象
// Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
// Microsoft.Office.Interop.Excel.Workbook workBook = excel.Application.Workbooks.Add(true);
// excel.Visible = false;//是否打开该Excel文件
// //填充数据
// for (int i = 0; i < rowNumber; i++)
// {
// for (int j = 0; j < columnNumber; j++)
// {
// excel.Cells[i + 1, j + 1] = dataTable.Rows[i].ItemArray[j];
// }
// }
// //string fileName = path + "\\" + DateTime.Now.ToString().Replace(':', '_') + ".xls";
// workBook.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
// try
// {
// workBook.Saved = true;
// excel.UserControl = false;
// //excelapp.Quit();
// }
// catch (Exception exception)
// {
// MessageBox.Show(exception.Message);
// }
// finally
// {
// workBook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Missing.Value, Missing.Value);
// excel.Quit();
// }
// if (isShowExcle)
// {
// System.Diagnostics.Process.Start(fileName);
// }
// return true;
//}
//public bool DataSetToExcel(DataSet dataSet, string fileName, bool isShowExcle)
//{
// DataTable dataTable = dataSet.Tables[0];
// int rowNumber = dataTable.Rows.Count;//不包括字段名
// int columnNumber = dataTable.Columns.Count;
// int colIndex = 0;
// if (rowNumber == 0)
// {
// MessageBox.Show("没有任何数据可以导入到Excel文件!");
// return false;
// }
// //建立Excel对象
// Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
// //excel.Application.Workbooks.Add(true);
// Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
// Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
// excel.Visible = isShowExcle;
// //Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.Worksheets[1];
// worksheet.Name = "挠度数据";
// Microsoft.Office.Interop.Excel.Range range;
// //生成字段名称
// foreach (DataColumn col in dataTable.Columns)
// {
// colIndex++;
// excel.Cells[1, colIndex] = col.ColumnName;
// }
// object[,] objData = new object[rowNumber, columnNumber];
// for (int r = 0; r < rowNumber; r++)
// {
// for (int c = 0; c < columnNumber; c++)
// {
// objData[r, c] = dataTable.Rows[r][c];
// }
// //Application.DoEvents();
// }
// // 写入Excel
// range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]);
// //range.NumberFormat = "@";//设置单元格为文本格式
// range.Value2 = objData;
// worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, 1]).NumberFormat = "yyyy-m-d h:mm";
// //string fileName = path + "\\" + DateTime.Now.ToString().Replace(':', '_') + ".xls";
// workbook.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
// try
// {
// workbook.Saved = true;
// excel.UserControl = false;
// //excelapp.Quit();
// }
// catch (Exception exception)
// {
// MessageBox.Show(exception.Message);
// }
// finally
// {
// workbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Missing.Value, Missing.Value);
// excel.Quit();
// }
// //if (isShowExcle)
// //{
// // System.Diagnostics.Process.Start(fileName);
// //}
// return true;
//}
/// <summary>
/// 将数据集中的数据保存到EXCEL文件
/// </summary>
/// <param name="dataSet">输入数据集</param>
/// <param name="fileName">保存EXCEL文件的绝对路径名</param>
/// <param name="isShowExcle">是否打开EXCEL文件</param>
/// <returns></returns>
public bool DataSetToExcel(DataSet dataSet, string fileName, bool isShowExcle)
{
DataTable dataTable = dataSet.Tables[0];
int rowNumber = dataTable.Rows.Count;//不包括字段名
int columnNumber = dataTable.Columns.Count;
int colIndex = 0;
if (rowNumber == 0)
{
MessageBox.Show("没有任何数据可以导入到Excel文件!");
return false;
}
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//excel.Application.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
excel.Visible = false;
//Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range;
//生成字段名称
foreach (DataColumn col in dataTable.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
object[,] objData = new object[rowNumber, columnNumber];
for (int r = 0; r < rowNumber; r++)
{
for (int c = 0; c < columnNumber; c++)
{
objData[r, c] = dataTable.Rows[r][c];
}
//Application.DoEvents();
}
// 写入Excel
range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]);
//range.NumberFormat = "@";//设置单元格为文本格式
range.Value2 = objData;
worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, 1]).NumberFormat = "yyyy-m-d h:mm";
//string fileName = path + "\\" + DateTime.Now.ToString().Replace(':', '_') + ".xls";
workbook.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
try
{
workbook.Saved = true;
excel.UserControl = false;
//excelapp.Quit();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
finally
{
workbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Missing.Value, Missing.Value);
excel.Quit();
}
if (isShowExcle)
{
System.Diagnostics.Process.Start(fileName);
}
return true;
}
}
}
来源:http://www.cnblogs.com/yellowcool/p/7447116.html
标签:C#,Excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现最小生成树算法详解
2023-11-25 04:51:22
![](https://img.aspxhome.com/file/2023/8/59678_0s.png)
Spring spel表达式使用方法示例
2023-08-25 00:43:32
SpringBoot统一响应格式及统一异常处理
2022-08-30 08:03:08
![](https://img.aspxhome.com/file/2023/5/63155_0s.png)
C++实现企业职工工资管理系统
2023-11-02 17:34:34
java15新功能的详细讲解
2023-08-23 04:40:21
详解Java线程-守护线程与用户线程
2023-11-25 00:17:29
Mybatis逆向工程运行代码实例
2021-06-22 04:54:59
![](https://img.aspxhome.com/file/2023/4/62144_0s.png)
java 用itext设置pdf纸张大小操作
2022-10-09 15:00:43
java中 ${} 和 #{} 有什么区别
2023-11-29 01:34:32
![](https://img.aspxhome.com/file/2023/5/67015_0s.png)
IDEA如何进行全局搜索图文教程
2022-10-14 13:39:45
![](https://img.aspxhome.com/file/2023/4/63684_0s.jpg)
详解springboot之jackson的两种配置方式
2021-11-03 11:01:21
java中断线程的正确姿势完整示例
2023-01-27 06:33:58
![](https://img.aspxhome.com/file/2023/8/67648_0s.jpg)
JDK1.8中ArrayList是如何扩容的
2023-01-20 19:04:47
![](https://img.aspxhome.com/file/2023/0/62700_0s.png)
springboot项目打成war包部署到tomcat遇到的一些问题
2023-10-12 12:46:46
在IntelliJ IDEA中使用gulp的方法步骤(图文)
2022-10-12 06:29:08
![](https://img.aspxhome.com/file/2023/0/60770_0s.png)
java的main方法中调用spring的service方式
2023-03-29 00:12:16
JSON复杂数据处理之Json树形结构数据转Java对象并存储到数据库的实现
2023-09-17 17:03:59
Java新手环境搭建 Tomcat安装配置教程
2021-08-19 08:06:23
![](https://img.aspxhome.com/file/2023/4/61294_0s.jpg)
Java字符串split使用方法代码实例
2023-02-06 18:55:31
![](https://img.aspxhome.com/file/2023/2/68002_0s.png)
java递归实现汉诺塔步骤介绍
2022-03-03 09:44:17
![](https://img.aspxhome.com/file/2023/1/64411_0s.png)