我正在为我的女朋友编写一个评分程序,但我一直试图将数据输出到嵌入程序中的 Excel 文件中。我目前已将其写入空白 Excel 文件,但想使用预制 Excel 文件并将数据导出到适当的单元格。我不知道如何告诉程序使用资源文件夹中的 xls 文件而不是制作空白的 excel 文件。这是迄今为止保存它的代码。我正在使用 C# 2008 Express 版本。
Thanks
我的来源参考是:Properties.Resources.grade 表
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//add data to excel
xlWorkSheet.Cells[1, 1] = firstName;
xlWorkSheet.Cells[2, 1] = lastName;
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
这应该可以帮助你。 (在.NET 4.5 中测试)。
public void openExcelTemplateFromResources ()
{
string tempPath = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllBytes(tempPath, Properties.Resources.excelResource);
Excel.Application excelApplication = new Excel.Application();
Excel._Workbook excelWorkbook;
excelWorkbook = excelApplication.Workbooks.Open(tempPath)
excelApplication.Visible = true; // at this point its up to the user to save the file
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)