关于C#winform向指定excel中的指定单元格添加数据的问题
我想做一个窗体,通过在窗体上一些文本的输入,然后点击确定,将数据输入到指定excel中的指定单元格中,希望知道的朋友麻烦告一下,最好详细一点,谢谢了。...
我想做一个窗体,通过在窗体上一些文本的输入,然后点击确定,将数据输入到指定excel中的指定单元格中,希望知道的朋友麻烦告一下,最好详细一点,谢谢了。
展开
1个回答
展开全部
private void button1_Click(object sender, EventArgs e)
{
try
{
string path;
MSExcel.Application excelapp;
MSExcel.Workbook exceldoc;
path = @"e:1.xlsx";
excelapp = new MSExcel.ApplicationClass();
if (File.Exists(path))
{
File.Delete(path);
}
object nothing = Missing.Value;
exceldoc = excelapp.Workbooks.Add(nothing);
MSExcel.Worksheet ws = (MSExcel.Worksheet)exceldoc.Sheets[1];
MSExcel.Range r;
MSExcel.Range r1;
r = ws.get_Range("B1", "B1");
r.Value2 = sudulabel.Text;
r1 = ws.get_Range("B2", "B2");
r1.Value2 = lichenglabel.Text;
object format = MSExcel.XlFileFormat.xlWorkbookDefault;
exceldoc.SaveAs(path, format, nothing, nothing, nothing, nothing, MSExcel.XlSaveAsAccessMode.xlExclusive, nothing, nothing, nothing, nothing, nothing);
exceldoc.Close(nothing, nothing, nothing);
excelapp.Quit();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
{
try
{
string path;
MSExcel.Application excelapp;
MSExcel.Workbook exceldoc;
path = @"e:1.xlsx";
excelapp = new MSExcel.ApplicationClass();
if (File.Exists(path))
{
File.Delete(path);
}
object nothing = Missing.Value;
exceldoc = excelapp.Workbooks.Add(nothing);
MSExcel.Worksheet ws = (MSExcel.Worksheet)exceldoc.Sheets[1];
MSExcel.Range r;
MSExcel.Range r1;
r = ws.get_Range("B1", "B1");
r.Value2 = sudulabel.Text;
r1 = ws.get_Range("B2", "B2");
r1.Value2 = lichenglabel.Text;
object format = MSExcel.XlFileFormat.xlWorkbookDefault;
exceldoc.SaveAs(path, format, nothing, nothing, nothing, nothing, MSExcel.XlSaveAsAccessMode.xlExclusive, nothing, nothing, nothing, nothing, nothing);
exceldoc.Close(nothing, nothing, nothing);
excelapp.Quit();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询