using System.Reflection;
using Microsoft.Office.Interop.Excel;
/// <summary>
/// 数据保存为Excel文件
/// </summary>
private void btnSaveAsExcel_Click(object sender, EventArgs e)
{
this.saveFileDialog.DefaultExt = "xlsx";
this.saveFileDialog.Filter = "Excel File(*.xlsx)|*.xlsx";
//选择保存路径
if (this.saveFileDialog.ShowDialog() != DialogResult.OK) return;
//创建一个Excel应用程序
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//是否显示导出过程(显示创建后的Excel)
excel.Visible = false;
//定义缺省值
Missing miss = Missing.Value;
//创建Excel文件
Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(miss);
List<Microsoft.Office.Interop.Excel.Worksheet> worksheetList = ToWorkSheetList(workbook);
excel.DisplayAlerts = false;
//保存文件
excel.ActiveWorkbook.SaveCopyAs(this.saveFileDialog.FileName);
//关闭表格
workbook.Close(false, miss, miss);
workbooks.Close();
//释放资源
excel.Quit();
MessageBox.Show("数据导出成功!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 多个工作表单
/// </summary>
public List<Microsoft.Office.Interop.Excel.Worksheet> ToWorkSheetList(Microsoft.Office.Interop.Excel.Workbook workbook)
{
string[] titles = new string[] { "201401", "201402", "201403" };
List<Microsoft.Office.Interop.Excel.Worksheet> worksheetList = new List<Microsoft.Office.Interop.Excel.Worksheet>();
for (int k = 0; k < titles.Length; k++)
{
Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Worksheets[k + 1] as Microsoft.Office.Interop.Excel.Worksheet;
sheet.Name = titles[k];
//循环添加列名
for (int i = 0; i < this.dgvData.Rows[0].Cells.Count; i++)
{
sheet.Cells[1, i + 1] = this.dgvData.Columns[i].HeaderText.ToString();
}
//填充数据
for (int i = 0; i < this.dgvData.Rows.Count; i++) //所要添加的行数
{
for (int j = 0; j < this.dgvData.Rows[0].Cells.Count; j++) //每行的列数
{
//将数据填充到对应的单元格中
sheet.Cells[i + 2, j + 1] = Convert.ToString(this.dgvData[j, i].Value);
}
}
//设置表格样式--列标题的背景颜色
Microsoft.Office.Interop.Excel.Range range=sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, this.dgvData.Columns.Count]);
range.Cells.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);
//重新选择单元格范围
int rowscount = this.dgvData.Rows.Count;
int columncount = this.dgvData.Columns.Count;
//将范围重新确定为每一行的每一个单元格
range = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[rowscount + 1, 1]);
//设置范围内的单元格的背景颜色为淡蓝色
range.Cells.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);
//选中Excel所有表格
range = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[rowscount + 1, columncount]);
//让Excel中的所有单元格的列宽自动调整
range.EntireColumn.AutoFit();
//让Excel的文本水平居中方式
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//range.Merge(0);
//将文字的颜色设为红色
//range.Font.Color = ColorTranslator.ToOle(Color.Red);
worksheetList.Add(sheet);
}
return worksheetList;
}
using Microsoft.Office.Interop.Excel;
/// <summary>
/// 数据保存为Excel文件
/// </summary>
private void btnSaveAsExcel_Click(object sender, EventArgs e)
{
this.saveFileDialog.DefaultExt = "xlsx";
this.saveFileDialog.Filter = "Excel File(*.xlsx)|*.xlsx";
//选择保存路径
if (this.saveFileDialog.ShowDialog() != DialogResult.OK) return;
//创建一个Excel应用程序
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//是否显示导出过程(显示创建后的Excel)
excel.Visible = false;
//定义缺省值
Missing miss = Missing.Value;
//创建Excel文件
Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(miss);
List<Microsoft.Office.Interop.Excel.Worksheet> worksheetList = ToWorkSheetList(workbook);
excel.DisplayAlerts = false;
//保存文件
excel.ActiveWorkbook.SaveCopyAs(this.saveFileDialog.FileName);
//关闭表格
workbook.Close(false, miss, miss);
workbooks.Close();
//释放资源
excel.Quit();
MessageBox.Show("数据导出成功!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 多个工作表单
/// </summary>
public List<Microsoft.Office.Interop.Excel.Worksheet> ToWorkSheetList(Microsoft.Office.Interop.Excel.Workbook workbook)
{
string[] titles = new string[] { "201401", "201402", "201403" };
List<Microsoft.Office.Interop.Excel.Worksheet> worksheetList = new List<Microsoft.Office.Interop.Excel.Worksheet>();
for (int k = 0; k < titles.Length; k++)
{
Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Worksheets[k + 1] as Microsoft.Office.Interop.Excel.Worksheet;
sheet.Name = titles[k];
//循环添加列名
for (int i = 0; i < this.dgvData.Rows[0].Cells.Count; i++)
{
sheet.Cells[1, i + 1] = this.dgvData.Columns[i].HeaderText.ToString();
}
//填充数据
for (int i = 0; i < this.dgvData.Rows.Count; i++) //所要添加的行数
{
for (int j = 0; j < this.dgvData.Rows[0].Cells.Count; j++) //每行的列数
{
//将数据填充到对应的单元格中
sheet.Cells[i + 2, j + 1] = Convert.ToString(this.dgvData[j, i].Value);
}
}
//设置表格样式--列标题的背景颜色
Microsoft.Office.Interop.Excel.Range range=sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, this.dgvData.Columns.Count]);
range.Cells.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);
//重新选择单元格范围
int rowscount = this.dgvData.Rows.Count;
int columncount = this.dgvData.Columns.Count;
//将范围重新确定为每一行的每一个单元格
range = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[rowscount + 1, 1]);
//设置范围内的单元格的背景颜色为淡蓝色
range.Cells.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);
//选中Excel所有表格
range = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[rowscount + 1, columncount]);
//让Excel中的所有单元格的列宽自动调整
range.EntireColumn.AutoFit();
//让Excel的文本水平居中方式
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//range.Merge(0);
//将文字的颜色设为红色
//range.Font.Color = ColorTranslator.ToOle(Color.Red);
worksheetList.Add(sheet);
}
return worksheetList;
}