Excel.Application mydataApp = new Excel.Application();
mydataApp.Visible =false;
mydataApp.DisplayAlerts=false;
Workbooks workbooks = mydataApp.Workbooks;
_Workbook workbook =workbooks.Open(strFile,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
Open的时候总是听是无法访问xx.xls,我的代码是从另外一个项目中拷贝的,那里可以用,这里不知道怎么回事急死了,文件的属性也不是只读的...
解决方案 »
- c# 窗口之间的切换如何实现
- C# windows Form 时间控件
- 关于反编译的问题
- 知道类的名称(字符串),如何得到该类? 在线等,我我只有20分了
- c#窗体怎么才能设置成透明和半透明?--在线等...
- 把一个Dataset里的多个DataTable导出到EXCEL里的多个Sheet里,并下载
- 关于winform的复选框checkedListBox的问题!
- 关于C#中给char数据类型覆值的问题。十万火急!
- 《C#网络应用案件例导航》 谁知道这本书附带光盘代码在哪能下载?
- 谁帮我看一下文件路径名的问题?
- 需要Html Dom Parser
- 如何将VS2008的界面操作习惯从VB.net改成C#.net?
{
if (gridView.Rows.Count == 0)
return false;
//建立EXCEL对象
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成地址字段名
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i <=gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + gridView[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
}
return true;
} private void button3_Click(object sender, EventArgs e)
{
this.ExportDataGridview(dataGridView1, true);
}你看这个可以吗, 我也是刚从书上弄过的, 能导到EXCEL中的
public void ImportData()
{
string strFileName; Microsoft.Office.Interop.Excel.ApplicationClass MyExcel;
Microsoft.Office.Interop.Excel.Workbooks wbs;
Microsoft.Office.Interop.Excel.Workbook wb;
Microsoft.Office.Interop.Excel.Worksheet ws; this.ImportFileDialog = new System.Windows.Forms.OpenFileDialog(); ImportFileDialog.Filter = "Excel Files(*.xls)|*.xls";
if (ImportFileDialog.ShowDialog() == DialogResult.OK)
if (ImportFileDialog.CheckFileExists)
{
strFileName = ImportFileDialog.FileName; MyExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); wbs = MyExcel.Workbooks;
wb = wbs.Open(strFileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing); ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1]; int Row = EXCEL_START_ROW; while (true)
{
bool BlankRow = true;
string CellData; string ErrMsg;
bool[] Err; DataTable.RowInfo NewRow = this.NewRow(); for (int i = 0; i < ColInfos.Length; i++)
{
if (((Microsoft.Office.Interop.Excel.Range)ws.Cells[Row, i + 1]).Value2 == null)
CellData = "";
else
CellData = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[Row, i + 1]).Value2.ToString(); if (CellData != "")
BlankRow = false;
//caizy2008/12/26改 日期从EXCEL读入的数值转换成日期------------------------------------
if (ColInfos[i].DataType == DataTable.CellType.DATETIME && CellData!="")
{
//DateTime theDate = new DateTime(long.Parse(CellData));
DateTime theDate =DateTime.FromOADate(double.Parse(CellData));
NewRow.Data[i] = theDate.ToString();
//Convert.ToDateTime("19000100").Date.AddDays(double.Parse(CellData));
}
else
{
NewRow.Data[i] = CellData;
}
//caizy2008/12/26改------------------------------------ } if (BlankRow)
break; for (int i = 0; i < ColInfos.Length; i++)
{
if (ColInfos[i].Level==0 && NewRow.Data[i]=="")
{
NewRow.Data[i] = Common.IdControl.GetNewId(dataSource, ColInfos[i].ColId);
}
} ddc = new DataDealCenter.dataDealCenter();
ddc.Mode = DataDealCenter.dataDealCenter.OPERATE_MODE.NEW; if (ddc._Check(this, null, NewRow, out Err, out ErrMsg, false))
ddc._Update(this, null, NewRow, out ErrMsg, false); Row++;
} wb.Saved = true;
//MyExcel.Save(strFileName);
wb.Close(Type.Missing, Type.Missing, Type.Missing);
MyExcel.Quit(); Common.MessageControl.ShowMsg(10074, MessageControl.MSG_TYPE.INFOMATION);
}
}