c#使用EXECL的问题
大家好!我在用Excel.Application xlApp = new Excel.Application();导出数据到EXECL里面的时候,当生成文件后,怎么以导出就出现这个提示,请哪位知道的指导一下可以吗?谢谢
就如下面的代码
Excel.Application app = new Excel.ApplicationClass();
if( app == null)
{
MessageBox.Show( "Excel无法启动 ");
return;
}
app.Visible = true;
Excel.Workbooks wbs = app.Workbooks;
Excel.Workbook wb = wbs.Add(Missing.Value);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
Excel.Range r = ws.get_Range( "A1 ", "H1 ");
object [] objHeader = { "标题1 ", "标题2 ", "标题3 ",
"标题4 ", "标题5 ", "标题6 ",
"标题7 ", "标题8 "};
r.Value = objHeader;
if (lv.Items.Count > 0)
{
r = ws.get_Range( "A2 ",Missing.Value);
object [,] objData = new Object[this.lv.Items.Count,8];
foreach(ListViewItem lvi in lv.Items)
{
objData[lvi.Index,0] = lvi.Text;
objData[lvi.Index,1] = lvi.SubItems[1].Text;
objData[lvi.Index,2] = lvi.SubItems[2].Text;
objData[lvi.Index,3] = lvi.SubItems[3].Text;
objData[lvi.Index,4] = lvi.SubItems[4].Text;
objData[lvi.Index,5] = lvi.SubItems[5].Text;
objData[lvi.Index,6] = lvi.SubItems[6].Text;
objData[lvi.Index,7] = lvi.SubItems[7].Text;
}
r = r.get_Resize(lv.Items.Count,8);
r.Value = objData;
r.EntireColumn.AutoFit();
}
app = null;
我在VS调试的时候是没有问题的,但是生成EXE文件后,就不能导入了,出现下面的错误:
我在论坛里面也提过这个问题,有人多是少了dll什么的,可是我还没有找到原因,请哪位知道的指点一下可以吗?这个问题已经
困了我很久了,谢谢
--------------------编程问答-------------------- 项目右键-引用-添加引用-.net-Microsoft.Office.interop.Excel
我的是2008,2005的话再找找…… --------------------编程问答-------------------- http://blog.csdn.net/changjiangzhibin/archive/2008/04/18/2305862.aspx
--------------------编程问答-------------------- ding --------------------编程问答-------------------- 权限问题也需要调整下。。关于EXECL。DLL的使用权限 --------------------编程问答-------------------- 添加EXCEL的引用! --------------------编程问答--------------------
//Com Microsoft Excel 12.0 Object Library //2007 ,11.0-2003
using MSExcel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;
Class Program
{
static void Main(string[] args(
{
string path; //文件路径
MSExcel.Application excelApp; //Excel应用程序
MSExcel.WorkBook excelDoc; //Excel文档
path = @"c:\test.xlsx";
excelApp = new MSExcel.ApplicationClass();
if(File.Exists(path)
{
File.Delete(path);
}
Object nothing = Missing.Value;
excelDoc = excelApp.Workbooks.Add(nothing);
Object format = MSExcel.XlFileFormat.xlWorkbookDefault;
exceldoc.SaveAs(path,nothing,nothing,nothing,nothing,nothing,
MSExcel.XlSaveAsAccessMode.xlExclusive,nothing,nothing,nothing,nothing,nothing);
excelDoc.Close(nothing,nothing,nothing);
excelApp.Quit();
}
}
请问权限应该怎么调整? --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms ;
using System.IO;
namespace HG_Wage
{
class ExportXLS
{
#region 定义把DataGridView中的数据导出到Excel表格的方法
/// <summary>
/// 定义把DataGridView中的数据导出到Excel表格的方法
/// </summary>
/// <param name="DataGridView1">DataGridView控件名</param>
public static void ExportDataGridViewToExcel(DataGridView DataGridView1)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls) |*.xls|所有文件(*.*)|*.*|纯文本(*.txt)|*.txt|HTML(*.html)|*.html|RTF格式(*.RTF)|*.rtf|CSV格式(*.csv)|*.csv";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出Excel文件到 ";
//随机生成文件名系统时间到秒.如:20080413-093345
DateTime now = DateTime.Now;
saveFileDialog.FileName = now.Year.ToString().PadLeft(2)
+ now.Month.ToString().PadLeft(2, '0')
+ now.Day.ToString().PadLeft(2, '0') + "-"
+ now.Hour.ToString().PadLeft(2, '0')
+ now.Minute.ToString().PadLeft(2, '0')
+ now.Second.ToString().PadLeft(2, '0');
//saveFileDialog.ShowDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("GB2312"));
string str = " ";
try
{
//写标题
for (int i = 0; i < DataGridView1.ColumnCount; i++)
{
if (i > 0)
{
str += "\t ";
}
str += DataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < DataGridView1.RowCount - 1; j++)
{
string tempStr = " ";
for (int k = 0; k < DataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t ";
}
tempStr += DataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
}
#endregion
}
}
================
再引用一下就可以了... --------------------编程问答-------------------- 有什么再好的欢迎M我哦...我感觉这个类写的还好... --------------------编程问答-------------------- 不管上面的有没有解决,不过使用cjc1021 的类确实实现了所要求的功能 谢谢
补充:.NET技术 , C#