C#导出EXCEL的几种方法
生活随笔
收集整理的這篇文章主要介紹了
C#导出EXCEL的几种方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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("沒有任何數(shù)據(jù)可以導(dǎo)入到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文件// //填充數(shù)據(jù)
// 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;
// }// //填充數(shù)據(jù)
// 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>
/// 將數(shù)據(jù)集中的數(shù)據(jù)導(dǎo)出到EXCEL文件
/// </summary>
/// <param name="dataSet">輸入數(shù)據(jù)集</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 = "@";//設(shè)置單元格為文本格式
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("沒有任何數(shù)據(jù)可以導(dǎo)入到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文件// //填充數(shù)據(jù)
// 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("沒有任何數(shù)據(jù)可以導(dǎo)入到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文件// //填充數(shù)據(jù)
// 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("沒有任何數(shù)據(jù)可以導(dǎo)入到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 = "撓度數(shù)據(jù)";
// 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 = "@";//設(shè)置單元格為文本格式
// 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>
/// 將數(shù)據(jù)集中的數(shù)據(jù)保存到EXCEL文件
/// </summary>
/// <param name="dataSet">輸入數(shù)據(jù)集</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("沒有任何數(shù)據(jù)可以導(dǎo)入到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 = "@";//設(shè)置單元格為文本格式
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;
}
}
}
?
總結(jié)
以上是生活随笔為你收集整理的C#导出EXCEL的几种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用 Nginx 代理 Socket.i
- 下一篇: 项目乱码 GBK转UTF-8工具