各位大哥:
我现使用VS2010 旗舰版,在编写一段代码实现将数组的内容写入一个EXCEL文件中的不同Sheet表里时,总是失败,请大家能否给一段成功运行的代码呀?谢谢了。下面贴上我的代码段,也请大家帮忙看看为什么失败?
private void createxcel()
{
Excel.Application xlapp1 = default(Excel.Application);
//Excel对象
Excel.Workbook xlbook1 = default(Excel.Workbook);
//工作簿
Excel.Worksheet xlsheet1 = default(Excel.Worksheet);
//工作表
try
{
xlapp1 = new Excel.Application();
xlapp1.Visible = false;
xlapp1.DisplayAlerts = false;
xlapp1.ShowWindowsInTaskbar = false;
xlbook1 = xlapp1.Workbooks.Add() ;
//新建EXCEL工件簿文件
xlsheet1 = xlbook1.Worksheets[1];//这句代码总是出错,无法通过编译,该怎么写呢?
xlsheet1.Cells[1, 1] = "流水号";
xlsheet1.Cells[1, 1] = "流水号";
xlsheet1.Cells[1, 2] = "名称";
xlsheet1.Cells[1, 3] = "主题";
xlsheet1.Cells[1, 4] = "时间";
xlsheet1.Cells[1, 5] = "时限";
xlsheet1.Cells[1, 6] = "状态";
xlsheet1.Cells[1, 7] = "时间";
xlsheet1.Cells[1, 20] = "地址";
xlsheet1.Name = DateTime.Now.Year.ToString() + "年";
xlsheet1.Cells.Font.Size = 10;
xlsheet1.Cells.Font.Bold = false; xlsheet1 = xlbook1.Worksheets[2];//和上面的sheet不同。
xlsheet1.Cells[1, 1] = "流水号";
xlsheet1.Cells[1, 2] = "主题";
xlsheet1.Cells[1, 3] = "时间";
xlsheet1.Cells[1, 4] = "时限";
xlsheet1.Cells[1, 5] = "状态";
xlsheet1.Cells[1, 6] = "时间";
xlsheet1.Cells[1, 20] = "地址";
xlsheet1.Name = DateTime.Now.Year.ToString() + "年";
xlsheet1.Cells.Font.Size = 10;
xlsheet1.Cells.Font.Bold = false; xlbook1.SaveAs(datafile + ".xls");
//保存文件 }
catch (Exception ex)
{
log(ex.Message.ToString());
}
finally
{
xlapp1.Workbooks.Close();
xlapp1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp1);
xlsheet1 = null;
xlbook1 = null;
xlapp1 = null;
System.GC.Collect(0);
}
}
我现使用VS2010 旗舰版,在编写一段代码实现将数组的内容写入一个EXCEL文件中的不同Sheet表里时,总是失败,请大家能否给一段成功运行的代码呀?谢谢了。下面贴上我的代码段,也请大家帮忙看看为什么失败?
private void createxcel()
{
Excel.Application xlapp1 = default(Excel.Application);
//Excel对象
Excel.Workbook xlbook1 = default(Excel.Workbook);
//工作簿
Excel.Worksheet xlsheet1 = default(Excel.Worksheet);
//工作表
try
{
xlapp1 = new Excel.Application();
xlapp1.Visible = false;
xlapp1.DisplayAlerts = false;
xlapp1.ShowWindowsInTaskbar = false;
xlbook1 = xlapp1.Workbooks.Add() ;
//新建EXCEL工件簿文件
xlsheet1 = xlbook1.Worksheets[1];//这句代码总是出错,无法通过编译,该怎么写呢?
xlsheet1.Cells[1, 1] = "流水号";
xlsheet1.Cells[1, 1] = "流水号";
xlsheet1.Cells[1, 2] = "名称";
xlsheet1.Cells[1, 3] = "主题";
xlsheet1.Cells[1, 4] = "时间";
xlsheet1.Cells[1, 5] = "时限";
xlsheet1.Cells[1, 6] = "状态";
xlsheet1.Cells[1, 7] = "时间";
xlsheet1.Cells[1, 20] = "地址";
xlsheet1.Name = DateTime.Now.Year.ToString() + "年";
xlsheet1.Cells.Font.Size = 10;
xlsheet1.Cells.Font.Bold = false; xlsheet1 = xlbook1.Worksheets[2];//和上面的sheet不同。
xlsheet1.Cells[1, 1] = "流水号";
xlsheet1.Cells[1, 2] = "主题";
xlsheet1.Cells[1, 3] = "时间";
xlsheet1.Cells[1, 4] = "时限";
xlsheet1.Cells[1, 5] = "状态";
xlsheet1.Cells[1, 6] = "时间";
xlsheet1.Cells[1, 20] = "地址";
xlsheet1.Name = DateTime.Now.Year.ToString() + "年";
xlsheet1.Cells.Font.Size = 10;
xlsheet1.Cells.Font.Bold = false; xlbook1.SaveAs(datafile + ".xls");
//保存文件 }
catch (Exception ex)
{
log(ex.Message.ToString());
}
finally
{
xlapp1.Workbooks.Close();
xlapp1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp1);
xlsheet1 = null;
xlbook1 = null;
xlapp1 = null;
System.GC.Collect(0);
}
}
xlbook1.Worksheets[1];
xlbook1.Worksheets[1]这块下面显示红色的波浪号,并有提示“找不到编译动态表达式的一种或多种类型。是否缺少对microsoft.CSharp.dll和System.core.dll的引用”。这里我就不知道该怎么弄了。
private void SaveTable()
{
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.WorkbookClass ExcelWorkBook = (Microsoft.Office.Interop.Excel.WorkbookClass)ExcelApp.Workbooks.Open(Application.StartupPath + "\\mybook.xls", Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet ExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelWorkBook.Worksheets[1];
pm.GetEntitySize(pm.enumPowerMILLEntityType.pmModel, ModName, ref Xmin, ref Xmax, ref Ymin, ref Ymax, ref Zmin, ref Zmax);
ExcelWorkSheet.Cells[22, 5] = "X:" + ((Xmax - Xmin) / 2).ToString() + " Y:" + ((Ymax - Ymin) / 2).ToString() + " Z:" + (Zmax - Zmin).ToString();
//ExcelWorkSheet.Cells[5, 12] = Session;
//ExcelWorkSheet.Cells[32, 9] = Date;
//ExcelWorkSheet.Cells[2, 8] = Xmax;
//ExcelWorkSheet.Cells[2, 9] = Ymax;
//ExcelWorkSheet.Cells[2, 10] = Zmax;
//ExcelWorkSheet.Cells[3, 8] = Xmin;
//ExcelWorkSheet.Cells[3, 9] = Ymin;
//ExcelWorkSheet.Cells[3, 10] = Zmin;
ExcelWorkBook.Save();
ExcelApp.Quit();
ExcelApp = null;
ExcelWorkBook = null;
ExcelWorkSheet = null;
GC.Collect();
}
using System;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net.Mail;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;另外,我电脑上肯定是装了OFFICE2007呀。
OFFICE2007 是Microsoft.Office.Interop.Excel 12 版的
Workbook xlBook = xlApp.Workbooks.Add(true);第一个sheet
Worksheet wsMask = (Worksheet)xlBook.Worksheets["sheet1"];
wsMask.Columns.ColumnWidth = 2;
wsMask.Columns.RowHeight = 13.5;
wsMask.Name = "1";第二个sheet Worksheet wsElement = (Worksheet)xlBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
wsElement.Name = "2";第三个sheet Worksheet wsElement2 = (Worksheet)xlBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
wsElement2.Name = "3";。保存成07的 xlBook.SaveAs(outputFileName, 56, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);保存成03的
xlBook.SaveAs(outputFileName, 43, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);