请问有没有 C#导出EXCEL的实例吗? 请问有没有 C#导出EXCEL的实例吗?刚开始学习C#,可以把实例发到我邮箱吗?E_MAIL: [email protected] 谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 导出excel方法 /// </summary> /// <param name="title"></param> /// <param name="exprotGrid"></param> /// <returns></returns> public static string exprotExcel(string title, DataGridView exprotGrid) { string message = ""; if (exprotGrid.Rows.Count == 0) { message = "当前数据为空,不执行导出"; return message; } Excel.Application exlapp = new Excel.Application(); if (exlapp == null) { message = "excel无法启动"; return message; } Excel.Workbook exlbook = exlapp.Workbooks.Add(true); Excel.Worksheet exlsheet = (Excel.Worksheet)exlbook.Worksheets[1]; Excel.Range range = exlsheet.get_Range(exlapp.Cells[1, 1], exlapp.Cells[1, exprotGrid.ColumnCount]); range.MergeCells = true; exlapp.ActiveCell.FormulaR1C1 = title; exlapp.ActiveCell.Font.Size = 20; exlapp.ActiveCell.Font.Bold = true; exlapp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter; int colIndex = 0; int rowIndex = 0; int colCount = exprotGrid.ColumnCount; int rowCount = exprotGrid.RowCount; object[,] objdata = new object[rowCount + 1, colCount]; for (colIndex = 0; colIndex < colCount; colIndex++) { objdata[rowIndex, colIndex] = exprotGrid.Columns[colIndex].HeaderText; } for (rowIndex = 0; rowIndex < rowCount; rowIndex++) { for (colIndex = 0; colIndex < colCount; colIndex++) { if (exprotGrid[colIndex, rowIndex].Value != null) { if (exprotGrid[colIndex, rowIndex].Value.ToString().ToLower() == "true") { objdata[rowIndex + 1, colIndex] = "是"; } else if (exprotGrid[colIndex, rowIndex].Value.ToString().ToLower() == "false") { objdata[rowIndex + 1, colIndex] = "否"; } else { objdata[rowIndex + 1, colIndex] = exprotGrid[colIndex, rowIndex].Value.ToString(); } } else { objdata[rowIndex + 1, colIndex] = ""; } } Application.DoEvents(); } exlapp.get_Range(exlapp.Cells[2, 1], exlapp.Cells[2, colIndex]).Font.Bold = true; range = exlsheet.get_Range(exlapp.Cells[2, 1], exlapp.Cells[rowCount + 2, colCount]); range.Value2 = objdata; try { exlapp.Cells.EntireColumn.AutoFit(); exlapp.Cells.VerticalAlignment = Excel.Constants.xlCenter; exlapp.Cells.HorizontalAlignment = Excel.Constants.xlCenter; exlapp.Visible = true; } catch { message = "保存出错,请检查"; return message; } return message; } [align=left]/// <summary> ///导出Excel (参考) /// </summary> /// <param name="System.Data.DataTable excelTable">要导出的excel文件</param> /// <param name="filePath">路劲</param> /// <returns></returns> public void ExportExcel(DataSet tempds, string saveFileName) { if (tempds == null) { MessageBox.Show("要导出的数据为空!!!"); this.Close(); return; } Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel"); this.Close(); return; } try { xlApp.Visible = false; if (xlApp == null) { MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel"); this.Close(); return; } bool fileSaved = false; Workbooks workbooks = xlApp.Workbooks; Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet worksheet = (Worksheet)workbook.Worksheets[1];//取得sheet1 Range range; long totalCount = tempds.Tables[0].Rows.Count; long rowRead = 0; float percent = 0; for (int i = 0; i < tempds.Tables[0].Columns.Count; i++) { worksheet.Cells[2, i + 1] = tempds.Tables[0].Columns[i].ColumnName; range = (Range)worksheet.Cells[2, i + 1]; range.Interior.ColorIndex = 15; range.Font.Bold = true; } //写入数值 int[] stringFlags = new int[60]; for (int r = 0; r < tempds.Tables[0].Rows.Count; r++) { for (int i = 0; i < tempds.Tables[0].Columns.Count; i++)//判断有多少是字符类型 { if (r == 0) { string type = tempds.Tables[0].Columns[i].DataType.ToString(); if (type == "System.String") { stringFlags[i] = 1; range = (Range)worksheet.Cells[r + 3, i + 1]; range.NumberFormatLocal = "@"; } worksheet.Cells[r + 3, i + 1] = tempds.Tables[0].Rows[r][i]; } else { if (stringFlags[i] == 1)//对于字符类型的处理 { range = (Range)worksheet.Cells[r + 3, i + 1]; range.NumberFormatLocal = "@"; } worksheet.Cells[r + 3, i + 1] = tempds.Tables[0].Rows[r][i]; } } rowRead++; percent = ((float)(100 * rowRead)) / totalCount; Text = "正在导出数据[" + percent.ToString("0.00") + "%]..."; System.Windows.Forms.Application.DoEvents(); } if (saveFileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(saveFileName); fileSaved = true; } catch (Exception ex) { fileSaved = false; MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message); } } else { fileSaved = false; } } catch (Exception ee) { MessageBox.Show("导出发生错误,错误信息为:" + ee.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { xlApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null; MessageBox.Show("导出完毕"); } }[/align] .net remoting 服务器广播 客户端在内网 的问题 加入timer控件不执行,语句连接无误 word barcode 部署在window 2008 报错:类型不匹配 找不到菜单编缉器的控件? 关于装箱的问题 求一个存储过程代码? 怎样在打开文件对话框中加入另外的东西啊? Remoting:一个小问题希望能解释解释。 “System.Windows.Controls.TextBox”不包含“DataBindings”的定义是怎么回事? 请问高手在使用DataView的构造函数时第二个参数是过滤条件,如何才能使列值等于 谁有icba数据库, 求一个树形的代码
/// <summary>
/// 导出excel方法
/// </summary>
/// <param name="title"></param>
/// <param name="exprotGrid"></param>
/// <returns></returns>
public static string exprotExcel(string title, DataGridView exprotGrid)
{
string message = "";
if (exprotGrid.Rows.Count == 0)
{
message = "当前数据为空,不执行导出";
return message;
}
Excel.Application exlapp = new Excel.Application();
if (exlapp == null)
{
message = "excel无法启动";
return message;
}
Excel.Workbook exlbook = exlapp.Workbooks.Add(true);
Excel.Worksheet exlsheet = (Excel.Worksheet)exlbook.Worksheets[1]; Excel.Range range = exlsheet.get_Range(exlapp.Cells[1, 1], exlapp.Cells[1, exprotGrid.ColumnCount]);
range.MergeCells = true;
exlapp.ActiveCell.FormulaR1C1 = title;
exlapp.ActiveCell.Font.Size = 20;
exlapp.ActiveCell.Font.Bold = true;
exlapp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter; int colIndex = 0;
int rowIndex = 0;
int colCount = exprotGrid.ColumnCount;
int rowCount = exprotGrid.RowCount;
object[,] objdata = new object[rowCount + 1, colCount]; for (colIndex = 0; colIndex < colCount; colIndex++)
{
objdata[rowIndex, colIndex] = exprotGrid.Columns[colIndex].HeaderText;
}
for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
if (exprotGrid[colIndex, rowIndex].Value != null)
{
if (exprotGrid[colIndex, rowIndex].Value.ToString().ToLower() == "true")
{
objdata[rowIndex + 1, colIndex] = "是";
}
else if (exprotGrid[colIndex, rowIndex].Value.ToString().ToLower() == "false")
{
objdata[rowIndex + 1, colIndex] = "否";
}
else
{
objdata[rowIndex + 1, colIndex] = exprotGrid[colIndex, rowIndex].Value.ToString();
}
}
else
{
objdata[rowIndex + 1, colIndex] = "";
}
}
Application.DoEvents();
} exlapp.get_Range(exlapp.Cells[2, 1], exlapp.Cells[2, colIndex]).Font.Bold = true;
range = exlsheet.get_Range(exlapp.Cells[2, 1], exlapp.Cells[rowCount + 2, colCount]);
range.Value2 = objdata;
try
{
exlapp.Cells.EntireColumn.AutoFit();
exlapp.Cells.VerticalAlignment = Excel.Constants.xlCenter;
exlapp.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
exlapp.Visible = true;
}
catch
{
message = "保存出错,请检查";
return message;
}
return message;
}
///导出Excel (参考)
/// </summary>
/// <param name="System.Data.DataTable excelTable">要导出的excel文件</param>
/// <param name="filePath">路劲</param>
/// <returns></returns>
public void ExportExcel(DataSet tempds, string saveFileName)
{
if (tempds == null)
{
MessageBox.Show("要导出的数据为空!!!");
this.Close();
return;
}
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel");
this.Close();
return;
}
try
{
xlApp.Visible = false;
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel");
this.Close();
return;
}
bool fileSaved = false;
Workbooks workbooks = xlApp.Workbooks;
Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];//取得sheet1
Range range;
long totalCount = tempds.Tables[0].Rows.Count;
long rowRead = 0;
float percent = 0;
for (int i = 0; i < tempds.Tables[0].Columns.Count; i++)
{
worksheet.Cells[2, i + 1] = tempds.Tables[0].Columns[i].ColumnName;
range = (Range)worksheet.Cells[2, i + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true; }
//写入数值
int[] stringFlags = new int[60];
for (int r = 0; r < tempds.Tables[0].Rows.Count; r++)
{
for (int i = 0; i < tempds.Tables[0].Columns.Count; i++)//判断有多少是字符类型
{
if (r == 0)
{
string type = tempds.Tables[0].Columns[i].DataType.ToString();
if (type == "System.String")
{
stringFlags[i] = 1;
range = (Range)worksheet.Cells[r + 3, i + 1];
range.NumberFormatLocal = "@";
}
worksheet.Cells[r + 3, i + 1] = tempds.Tables[0].Rows[r][i]; }
else
{
if (stringFlags[i] == 1)//对于字符类型的处理
{
range = (Range)worksheet.Cells[r + 3, i + 1];
range.NumberFormatLocal = "@";
}
worksheet.Cells[r + 3, i + 1] = tempds.Tables[0].Rows[r][i]; }
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
Text = "正在导出数据[" + percent.ToString("0.00") + "%]...";
System.Windows.Forms.Application.DoEvents();
}
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
else
{
fileSaved = false;
}
}
catch (Exception ee)
{
MessageBox.Show("导出发生错误,错误信息为:" + ee.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
MessageBox.Show("导出完毕");
}
}[/align]