c# winform如何将数据存到指定excel表,并实时显示
展开全部
补充和小技巧:
using Excel = Microsoft.Office.Interop.Excel; // 简化命名空间
在窗体事件中执行:
var xlsApp = new Excel.Application();
var wb = xlsApp.Workbooks.Add();
var sh = wb.ActiveSheet as Excel.Worksheet;
sh.Range["A1"].Value = "C# -> Excel 示例";
for (int row = 2; row <= 10; row++)
{
for (int col = 1; col <= 10; col++)
{
sh.Cells[RowIndex: row, ColumnIndex: col].Value = string.Format("{0}, {1}", row, col);
}
}
// 保存文件到 C 盘,请根据自己的 Excel 版本确定 FileFormat
// sh.SaveAs(Filename: "C:\\test.xls", FileFormat: Excel.XlFileFormat.xlExcel12);
xlsApp.Visible = true;
// xlsApp.Quit();
using Excel = Microsoft.Office.Interop.Excel; // 简化命名空间
在窗体事件中执行:
var xlsApp = new Excel.Application();
var wb = xlsApp.Workbooks.Add();
var sh = wb.ActiveSheet as Excel.Worksheet;
sh.Range["A1"].Value = "C# -> Excel 示例";
for (int row = 2; row <= 10; row++)
{
for (int col = 1; col <= 10; col++)
{
sh.Cells[RowIndex: row, ColumnIndex: col].Value = string.Format("{0}, {1}", row, col);
}
}
// 保存文件到 C 盘,请根据自己的 Excel 版本确定 FileFormat
// sh.SaveAs(Filename: "C:\\test.xls", FileFormat: Excel.XlFileFormat.xlExcel12);
xlsApp.Visible = true;
// xlsApp.Quit();
展开全部
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
后面可以参考
//生成字段名称
for (int i = 0; i < DGVCustomer.ColumnCount; i++)
{
excel.Cells[1, i + 1] = DGVCustomer.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < DGVCustomer.RowCount - 1; i++)
{
for (int j = 0; j < DGVCustomer.ColumnCount; j++)
{
if (DGVCustomer[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + DGVCustomer[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = DGVCustomer[j, i].Value.ToString();
}
}
}
excel.Visible = true;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
后面可以参考
//生成字段名称
for (int i = 0; i < DGVCustomer.ColumnCount; i++)
{
excel.Cells[1, i + 1] = DGVCustomer.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < DGVCustomer.RowCount - 1; i++)
{
for (int j = 0; j < DGVCustomer.ColumnCount; j++)
{
if (DGVCustomer[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + DGVCustomer[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = DGVCustomer[j, i].Value.ToString();
}
}
}
excel.Visible = true;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询