把连接字符串改成这样就可以了。excel文件不用设置。
"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
解决方案 »
- C# 读取文件名 并且改变文件名
- VS2003+Developer Express .NET v3的项目在VS2005+Developer Express .NET v8.3编译出错
- Entity framework性能怎么样
- 新手怎么样才能快速入门,熟悉CSDN?
- 请问谁有Developer.Express.NET.WindowsForms.Component.Collection.with.Source.Code.v2.0.6
- 为什么tabpages.remove不起作用?在线等!
- 打包N天还没搞定。。。。不爽。散分,csdn太慢了,明白人太少了,发发牢骚。
- 怎么打开媒体播放器????
- C#版可用分回赠活动,只要问题提得好,你提问题我给分!
- C# List<T> 作为参数的问题
- strXH=this.txt_xh.Text.ToString().Trim();
- C#应用程序发布后,客户机不能查看水晶报表,怎么处理(急)
using Excel;
using System.Reflection;
using System.Windows.Forms;namespace 管理系统主程序
{
/// <summary>
/// SaveExcel 的摘要说明。
/// </summary>
public class SaveExcel
{
public SaveExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void TableSaveExcel(System.Data.DataTable tempTableName,string filename)
{
try
{
Excel.Application exc = new Excel.Application();
if (exc == null)
{
MessageBox.Show("不能建立EXCEL对象,请在机器上安装EXCEL","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
// exc.set_Visible(0, true);
// exc.Visible=true;
object MissValue = System.Reflection.Missing.Value;
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(MissValue);
// _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets; _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
// worksheet.Cells.
int i,numCols=tempTableName.Rows.Count,j;
int lnumCols = tempTableName.Columns.Count;
for (i = 0;i< lnumCols;i++) {
worksheet.Cells[1,i+1]=tempTableName.Columns[i].ColumnName;
}
string ttyy;
for (i = 0;i< numCols;i++) {
for (j = 0;j< lnumCols;j++) {
ttyy="'"+tempTableName.Rows[i][j].ToString();
worksheet.Cells[i+2,j+1]=ttyy;
}
}
worksheet.SaveAs(filename, MissValue, MissValue, MissValue, MissValue, MissValue, Excel.XlSaveAsAccessMode.xlNoChange , MissValue, MissValue);
workbooks.Close();
exc.Quit();
worksheet=null;workbooks=null;exc=null;GC.Collect();
}
catch
{
MessageBox.Show("导出至EXCEL错误!!!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
return ;
}
}
public void ViewSaveExcel(System.Data.DataView tempTableName,string filename)
{
try
{
// System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
// System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Excel.Application exc = new Excel.Application();
if (exc == null)
{
MessageBox.Show("不能建立EXCEL对象,请在机器上安装EXCEL","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
// exc.set_Visible(0, true);
// exc.Visible=true;
object MissValue = System.Reflection.Missing.Value;
Workbooks workbooks = exc.Workbooks;
// _Workbook workbook = workbooks.Add(MissValue);
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets; _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
// worksheet.Cells.
int iRowCount=tempTableName.Count;
int lnumCols = tempTableName.Table.Columns.Count;
for (int i = 0;i< lnumCols;i++) {
worksheet.Cells[1,i+1]=tempTableName.Table.Columns[i].ColumnName;
} Range rangedata;
// rangedata = worksheet.get_Range("A2", Missing.Value); Range xlRang = null;
int iParstedRow = 0, iCurrSize = 0;
int iEachSize = 2000; // each time you
object[,] objVal = new object[iEachSize, lnumCols];
try
{
iCurrSize = iEachSize;
while (iParstedRow < iRowCount)
{
if ((iRowCount - iParstedRow) < iEachSize)
iCurrSize = iRowCount - iParstedRow;
for (int i = 0; i < iCurrSize; i++)
{
for (int j = 0; j < lnumCols; j++)
objVal[i, j] ="'" +tempTableName[i + iParstedRow][j].ToString();
}
// Get Save Range from Excel WorkSheet
// such as A1 H10, means From A to H Columns, and 1 to 10 rows
xlRang = worksheet.get_Range("A" + ((int)(iParstedRow + 2)).ToString(), ((char)('A' + lnumCols - 1)).ToString() + ((int)(iParstedRow + iCurrSize + 1)).ToString()); xlRang.Value2 = objVal; iParstedRow = iParstedRow + iCurrSize;
} // change number format of the data column
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRang);
// System.Runtime.InteropServices.Marshal.ReleaseComObject(rangedata);
// xlRang = null;
// rangedata=null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return ;
}
// exc.Visible=true; worksheet.SaveAs(filename, MissValue, MissValue, MissValue, MissValue, MissValue, Excel.XlSaveAsAccessMode.xlNoChange , MissValue, MissValue);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRang);
workbooks.Close();
exc.Quit();
exc.Quit();
// System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
worksheet=null;workbooks=null;exc=null;xlRang = null;rangedata=null;GC.Collect();
// System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
}
catch
{
MessageBox.Show("导出至EXCEL错误!!!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
return ;
}
}
}
}
http://community.csdn.net/Expert/topic/5581/5581277.xml?temp=.3087885