DataSet导出到Excel(winform)
2012-09-05 14:17
337 查看
public static void ExportToExcel(DataSet dataSet,string fileName)
{
try
{
string saveFileName = string.Empty;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.DisplayAlerts = false;//保存时不提示
System.Diagnostics.Process[] xlPrcArray;
xlPrcArray = System.Diagnostics.Process.GetProcessesByName("EXCEL");
//确定新建Excel文件的进程
System.Diagnostics.Process newExcelPrc = xlPrcArray[0];
for (int i = 0; i <= xlPrcArray.Length - 1; i++)
{
if (xlPrcArray[i].TotalProcessorTime.TotalSeconds < newExcelPrc.TotalProcessorTime.TotalSeconds)
{
newExcelPrc = xlPrcArray[i];
}
}
// Create a new Excel Workbook
Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);
int sheetIndex = 0;
// Copy each DataTable
foreach (System.Data.DataTable dt in dataSet.Tables)
{
// Copy the DataTable to an object array
object[,] rawData = new object[dt.Rows.Count + 1, dt.Columns.Count];
// Copy the column names to the first row of the object array
for (int col = 0; col < dt.Columns.Count; col++)
{
rawData[0, col] = dt.Columns[col].ColumnName;
}
// Copy the values to the object array
for (int col = 0; col < dt.Columns.Count; col++)
{
//字符型前面要加上:'
if (dt.Columns[col].DataType == System.Type.GetType("System.String"))
{
for (int row = 0; row < dt.Rows.Count; row++)
{
rawData[row + 1, col] = "'" + dt.Rows[row].ItemArray[col].ToString();
}
}
else
{
for (int row = 0; row < dt.Rows.Count; row++)
{
rawData[row + 1, col] = dt.Rows[row].ItemArray[col].ToString();
}
}
}
// Calculate the final column letter
string finalColLetter = string.Empty;
string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int colCharsetLen = colCharset.Length;
if (dt.Columns.Count > colCharsetLen)
{
finalColLetter = colCharset.Substring(
(dt.Columns.Count - 1) / colCharsetLen - 1, 1);
}
finalColLetter += colCharset.Substring(
(dt.Columns.Count - 1) % colCharsetLen, 1);
// Create a new Sheet
Worksheet excelSheet = (Worksheet)excelWorkbook.Sheets.Add(
excelWorkbook.Sheets.get_Item(++sheetIndex),
Type.Missing, 1, XlSheetType.xlWorksheet);
excelSheet.Name = dt.TableName;
// Fast data export to Excel
string excelRange = string.Format("A1:{0}{1}",
finalColLetter, dt.Rows.Count + 1);
excelSheet.get_Range(excelRange, Type.Missing).Value2 = rawData;
// Mark the first row as BOLD
((Range)excelSheet.Rows[1, Type.Missing]).Font.Bold = true;
}
// Save and Close the Workbook
excelWorkbook.SaveAs(saveFileName, XlFileFormat.xlWorkbookNormal, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excelWorkbook.Close(true, Type.Missing, Type.Missing);
excelWorkbook = null;
//// Release the Application object
excelApp.Quit();
excelApp = null;
// Collect the unreferenced objects
GC.Collect();
GC.WaitForPendingFinalizers();
newExcelPrc.Kill();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
{
try
{
string saveFileName = string.Empty;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.DisplayAlerts = false;//保存时不提示
System.Diagnostics.Process[] xlPrcArray;
xlPrcArray = System.Diagnostics.Process.GetProcessesByName("EXCEL");
//确定新建Excel文件的进程
System.Diagnostics.Process newExcelPrc = xlPrcArray[0];
for (int i = 0; i <= xlPrcArray.Length - 1; i++)
{
if (xlPrcArray[i].TotalProcessorTime.TotalSeconds < newExcelPrc.TotalProcessorTime.TotalSeconds)
{
newExcelPrc = xlPrcArray[i];
}
}
// Create a new Excel Workbook
Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);
int sheetIndex = 0;
// Copy each DataTable
foreach (System.Data.DataTable dt in dataSet.Tables)
{
// Copy the DataTable to an object array
object[,] rawData = new object[dt.Rows.Count + 1, dt.Columns.Count];
// Copy the column names to the first row of the object array
for (int col = 0; col < dt.Columns.Count; col++)
{
rawData[0, col] = dt.Columns[col].ColumnName;
}
// Copy the values to the object array
for (int col = 0; col < dt.Columns.Count; col++)
{
//字符型前面要加上:'
if (dt.Columns[col].DataType == System.Type.GetType("System.String"))
{
for (int row = 0; row < dt.Rows.Count; row++)
{
rawData[row + 1, col] = "'" + dt.Rows[row].ItemArray[col].ToString();
}
}
else
{
for (int row = 0; row < dt.Rows.Count; row++)
{
rawData[row + 1, col] = dt.Rows[row].ItemArray[col].ToString();
}
}
}
// Calculate the final column letter
string finalColLetter = string.Empty;
string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int colCharsetLen = colCharset.Length;
if (dt.Columns.Count > colCharsetLen)
{
finalColLetter = colCharset.Substring(
(dt.Columns.Count - 1) / colCharsetLen - 1, 1);
}
finalColLetter += colCharset.Substring(
(dt.Columns.Count - 1) % colCharsetLen, 1);
// Create a new Sheet
Worksheet excelSheet = (Worksheet)excelWorkbook.Sheets.Add(
excelWorkbook.Sheets.get_Item(++sheetIndex),
Type.Missing, 1, XlSheetType.xlWorksheet);
excelSheet.Name = dt.TableName;
// Fast data export to Excel
string excelRange = string.Format("A1:{0}{1}",
finalColLetter, dt.Rows.Count + 1);
excelSheet.get_Range(excelRange, Type.Missing).Value2 = rawData;
// Mark the first row as BOLD
((Range)excelSheet.Rows[1, Type.Missing]).Font.Bold = true;
}
// Save and Close the Workbook
excelWorkbook.SaveAs(saveFileName, XlFileFormat.xlWorkbookNormal, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excelWorkbook.Close(true, Type.Missing, Type.Missing);
excelWorkbook = null;
//// Release the Application object
excelApp.Quit();
excelApp = null;
// Collect the unreferenced objects
GC.Collect();
GC.WaitForPendingFinalizers();
newExcelPrc.Kill();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
相关文章推荐
- 黑马程序员_WinForm导出DataSet到Excel中
- DataSet excel导出(winform)
- WinForm导出DataSet到Excel
- WinForm下DataGridView导出Excel的实现
- 导出DataSet数据到excel的多个sheet
- DataSet导出Excel,比以往的方法导出的Excel外观更加好看
- 整理的 C# DataSet导出至Excel文件(2)
- C# WinForm 导出导入Excel/Doc 完整实例教程
- 在asp.net中实现dataset与excel的相互导入导出
- C# WinForm导出Excel方法介绍
- c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- C# Winform 导出Excel
- Winform中datagridview导出Excel
- c#.net 使用NPOI导入导出标准Excel (asp.net winform csharp)
- C#导出Excel源码(WinForm)
- 利用OleDb方式对DataSet 和 Excel 数据快速导入导出
- DataSet导出到Excel比较完整的解决方案(一)--客户端生成文件(downmoon)
- 二十六、【开源框架】EFW框架Winform前端开发之Grid++Report报表、条形码、Excel导出、图表控件
- C# DataSet导出Excel
- C#合并excel单元格,操作合并后的单元格 (winform导出excel)