我正在创建Excel,如下所示:
class Program
{
private Excel.Application app = null;
private Excel.Application worksheet = null;
private Excel.Application workbook = null;
private Excel.Application worksheet_range = null;
//public Program()
//{
// CreateExcelDoc();
//}
static void Main(string[] args)
{
Program obj = new Program();
obj.CreateExcelDoc();
}
public void CreateExcelDoc()
{
try
{
app = new Excel.Application();
if (app == null)
return;
app.Visible = true;
workbook = app.Workbooks.Add(1);////error in this,saying "can not convert from Excel.Workbook to Excel.Application"
}
catch (Exception ex)
{
throw new Exception("CreateExcelDoc: Error" + ex.Message);
}
}
}
有谁可以告诉我哪里出错了? 注意:我正在关注此事:http://msdn.microsoft.com/en-us/library/ms173186%28v=vs.80%29.aspx
请发给我任何不错的链接/解决方案。我正在使用Windows 7 64位操作系统。
感谢。
答案 0 :(得分:3)
更改此声明:
private Excel.Application app = null;
private Excel.Application worksheet = null;
private Excel.Application workbook = null;
private Excel.Application worksheet_range = null;
到:
private Excel.Application app = null;
private Excel.Worksheet worksheet = null;
private Excel.Workbook workbook = null;
private Excel.Range worksheet_range = null;
您也可以完全遵循以下代码:
app.Visible = true; // or false if you don't want to show app.
workbook = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
worksheet = (Worksheet)workbook.Worksheets[1];
// Select the Excel cells, in the range c1 to c7 in the worksheet.
worksheet_range = worksheet.get_Range("C1", "C7");
修改强>
您可以使用以下代码保存工作簿:
workbook.SaveAs(@"C:\XMLCopy.xls",
Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
但在Workbook保存后查找资源发布方法。您需要关闭文件,应用程序和所有相关对象。