C#怎么把数据写入到.xls表格里单元格
1个回答
展开全部
/// <summary> 将DataGridView 写入ExceL中去
/// </summary>
/// <param name="dgv">保存的文件名称</param>
public void DataGridViewToExcel(DataGridView dgv, string name)
{
//总可见列数,总可见行数
int colCount = dgv.Columns.GetColumnCount(DataGridViewElementStates.Visible);
int rowCount = dgv.Rows.GetRowCount(DataGridViewElementStates.Visible);
//dataGridView 没有数据提示
if (dgv.Rows.Count == 0 || rowCount == 0) { MessageBox.Show("表中没有数据", "提示"); }
else
{ //选择创建文件的路径
SaveFileDialog save = new SaveFileDialog();
save.Filter = "excel files(*.xls)|*.xls";
save.Title = "请选择要导出数据的位置";
save.FileName = name + DateTime.Now.ToLongDateString();
if (save.ShowDialog() == DialogResult.OK)
{
string fileName = save.FileName; //MessageBox.Show(save.FileName);
// 创建Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//new Microsoft.Office.Interop.Excel.ApplicationClass();
if (excel == null) { MessageBox.Show("Excel无法启动", "提示"); return; }
//创建Excel工作薄
Microsoft.Office.Interop.Excel.Workbook excelBook = excel.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets[1];
//excel.Application.Workbooks.Add(true);
//生成字段名称
int k = 0; for (int i = 0; i < dgv.ColumnCount; i++)
{
if (dgv.Columns[i].Visible) //不导出隐藏的列
{ excel.Cells[1, k + 1] = dgv.Columns[i].HeaderText; k++; }
}
//填充数据
for (int i = 0; i < dgv.RowCount; i++)
{
k = 0; for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv.Columns[j].Visible)
//不导出隐藏的列
{
if (dgv[j, i].ValueType == typeof(string))
{ excel.Cells[i + 2, k + 1] = "" + dgv[j, i].Value.ToString(); }
else { excel.Cells[i + 2, k + 1] = dgv[j, i].Value.ToString(); }
} k++;
}
} try { excelBook.Saved = true; excelBook.SaveCopyAs(fileName); }
catch { MessageBox.Show("导出失败,文件可能正在使用中", "提示"); }
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询