如果是新建的Excel就没问题.
在网上查了下,好多都说是Excel的格式问题,但目前仍然没有找到解决的方法,希望各位高手指点一下
19 个解决方案
#1
从页面上导出的格式是一种兼容html的格式,你可以试试别的方法:
如导出成xml格式,或者不用导出,直接读取原数据
如导出成xml格式,或者不用导出,直接读取原数据
#2
直接读取原数据
#3
问题是出在Excel的格式上,调试下看看具体出错的位置
#4
会不会是你电脑上装的是excel2007啊,我想如果是2003应该不会报这样的错吧,只是猜测,当时我导出excel时,反正是电脑若是excel2007,导出时报错了
#5
是EXCEL格式的问题,应该定位一下问题出在哪,看一下具体错误信息或出错现场信息。
或者不使用OLE导入,直接以读取EXCEL单元格的形式导入。
或者不使用OLE导入,直接以读取EXCEL单元格的形式导入。
#6
以单元格形式导入,虽然效率差一些,但可以非常有效地避免空行、空列、列内容类型多内容读不进去(列内容有纯数字或纯字母内容时)的问题。
#7
#region 导入EXCEL
public static System.Data.DataTable ExcelToDS_manual(string filePath)
{
//创建Application对象
Excel.Application xApp = new Excel.ApplicationClass();
xApp.Visible = true;
Excel.Workbook xBook = xApp.Workbooks._Open(filePath,
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
int iColCount = 0;
int iRowCount = 0;
System.Data.DataTable dtExcel = new System.Data.DataTable();
//获取列数
string strColName;
for (int iColIndex = 1; ; iColIndex++)
{
if (null == ((Excel.Range)xSheet.Cells[1, iColIndex]).Value2)
{
break;
}
strColName = ((Excel.Range)xSheet.Cells[1, iColIndex]).Value2.ToString();
if (!string.IsNullOrEmpty(strColName))
{
iColCount++;
}
else
{
break;
}
System.Data.DataColumn newColumn = new DataColumn(strColName);
dtExcel.Columns.Add(newColumn);
}
int iNullRowCount = 0;
string strCellText;
for (int iRowIndex = 2; ; iRowIndex++)
{
if (null == ((Excel.Range)xSheet.Cells[iRowIndex, 1]).Value2)
{
if ((++iNullRowCount) >= 5)
{
break;
}
continue;
}
strCellText = ((Excel.Range)xSheet.Cells[iRowIndex, 1]).Value2.ToString();
if (!string.IsNullOrEmpty(strCellText))
{
iNullRowCount = 0;
}
else
{
if ((++iNullRowCount) >= 5)
{
break;
}
}
System.Data.DataRow newRow = dtExcel.NewRow();
for (int iColIndex = 1; iColIndex <= iColCount; iColIndex++)
{
if (null == ((Excel.Range)xSheet.Cells[iRowIndex, iColIndex]).Value2)
{
continue;
}
newRow[iColIndex - 1] = ((Excel.Range)xSheet.Cells[iRowIndex, iColIndex]).Value2.ToString();
}
dtExcel.Rows.Add(newRow);
}
xSheet = null;
xBook.Close(Missing.Value, Missing.Value, Missing.Value);
xBook = null;
xApp.Quit();
xApp = null;
return dtExcel;
}
#endregion
#8
//导出EXCEL示例:
Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
excel.Visible = true;
Worksheet sheet = ((Worksheet)excel.ActiveWorkbook.Sheets[1]);
int iRowCount = dataGridView1.Rows.Count;
int iColCount = dataGridView1.ColumnCount;
for (int k = 0; k < iColCount; k++)
{
excel.Cells[1, k + 1] = dataGridView1.Columns[k].HeaderText;
}
for (int i = 0; i < iRowCount; i++)
{
for (int j = 0; j < iColCount; j++)
{
excel.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value;
if ((1 == j) && ("★未匹配" == dataGridView1.Rows[i].Cells[j].Value))
{
sheet.get_Range(sheet.Cells[i + 2, j + 1], sheet.Cells[i + 2, j + 1]).Font.Color = System.Drawing.Color.Blue.ToArgb();
}
}
}
#9
上面的导出代码,不会将EXCEL保存到硬盘,它会打开一个新的EXCEL文件,然后往这个EXCEL文件中一行行写入数据,这个EXCEL文件需要用户自己保存。
#10
不能这样呀,我需要实现的是用户下载Excel,然后对里面的数据进行修改,在导入
报错信息:
System.Data.OleDb.OleDbException: 外部表不是预期的格式。 在 System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) 在 System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) 在 System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) 在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) 在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) 在 System.Data.OleDb.OleDbConnection.Open() 在 Netsource.btn_dr_Click(Object sender, EventArgs e) 位置 e:\InfoNet_zliuy\Netsource.aspx.cs:行号 1092
-------------------------------------------
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();//这里行号为1092
string sql = "select * from [Sheet1$]";
#11
学习一下
#12
protected void btnDownload_Click(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=Sheet1.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write("<table border='1'>");
Response.Write("<tr>");
Response.Write("<td align='center' colspan='4'>注:请注意大小写、全角与半角、-和_、:和;小区名称必须是:460-00-XXXXX-XXXXX</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>网元名</td>");
Response.Write("<td align='center'>基站名</td>");
Response.Write("<td align='center'>小区名</td>");
Response.Write("<td align='center'>是否为工程小区(默认为否)</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC442</td>");
Response.Write("<td align='center'>CDGBSC445:S14:</td>");
Response.Write("<td align='center'>460-00-29542-11441</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC443</td>");
Response.Write("<td align='center'>CDGBSC445:S14:</td>");
Response.Write("<td align='center'>460-00-29542-11442</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S14:</td>");
Response.Write("<td align='center'>460-00-29542-11443</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S22:</td>");
Response.Write("<td align='center'>460-00-29542-10221 </td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S22:</td>");
Response.Write("<td align='center'>460-00-29542-10222 </td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S22:</td>");
Response.Write("<td align='center'>460-00-29542-10223 </td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center' colspan='4'>以上为试例,请从下行可以填写</td>");
Response.Write("</tr>");
string SqlString = "select * from web_site";//查询表
try
{
DataTable dt = dbc.GetDataTable(SqlString);
foreach (DataRow Row in dt.Rows)
{
Response.Write("<tr>");
Response.Write("<td align='center'>" + Row["wy_name"].ToString() + "</td>");
Response.Write("<td align='center'>" + Row["jz_name"].ToString() + "</td>");
Response.Write("<td align='center'>" + Row["xq_name"].ToString() + "</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('"+ex.ToString()+"')</script>");
}
Response.Write("</table>");
Response.End();
}
这个是导出Excel的代码
#13
Mark !
#14
你用我那个7楼的代码导入试试,应该没什么问题。
我给你的是导入导出的代码。
#15
代码看起来蛮复杂的
#16
我在ASP.NET下导入excel到数据库sql2005时也遇到这个问题啦!
#17
匯出的資料是不是Html格式的啊 . 記得把檔案另存為.xls格式噢.....試一下
#18
操作 excel 2007 总是出现问题 好头疼
#19
OleConn.Open();我的程序在这里停了,不报错也不怎样?????
求高手解决!!!!!!!!!
反正xls文件是存在的!!!!!!!
求高手解决!!!!!!!!!
反正xls文件是存在的!!!!!!!
#20
#1
从页面上导出的格式是一种兼容html的格式,你可以试试别的方法:
如导出成xml格式,或者不用导出,直接读取原数据
如导出成xml格式,或者不用导出,直接读取原数据
#2
直接读取原数据
#3
问题是出在Excel的格式上,调试下看看具体出错的位置
#4
会不会是你电脑上装的是excel2007啊,我想如果是2003应该不会报这样的错吧,只是猜测,当时我导出excel时,反正是电脑若是excel2007,导出时报错了
#5
是EXCEL格式的问题,应该定位一下问题出在哪,看一下具体错误信息或出错现场信息。
或者不使用OLE导入,直接以读取EXCEL单元格的形式导入。
或者不使用OLE导入,直接以读取EXCEL单元格的形式导入。
#6
以单元格形式导入,虽然效率差一些,但可以非常有效地避免空行、空列、列内容类型多内容读不进去(列内容有纯数字或纯字母内容时)的问题。
#7
#region 导入EXCEL
public static System.Data.DataTable ExcelToDS_manual(string filePath)
{
//创建Application对象
Excel.Application xApp = new Excel.ApplicationClass();
xApp.Visible = true;
Excel.Workbook xBook = xApp.Workbooks._Open(filePath,
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
int iColCount = 0;
int iRowCount = 0;
System.Data.DataTable dtExcel = new System.Data.DataTable();
//获取列数
string strColName;
for (int iColIndex = 1; ; iColIndex++)
{
if (null == ((Excel.Range)xSheet.Cells[1, iColIndex]).Value2)
{
break;
}
strColName = ((Excel.Range)xSheet.Cells[1, iColIndex]).Value2.ToString();
if (!string.IsNullOrEmpty(strColName))
{
iColCount++;
}
else
{
break;
}
System.Data.DataColumn newColumn = new DataColumn(strColName);
dtExcel.Columns.Add(newColumn);
}
int iNullRowCount = 0;
string strCellText;
for (int iRowIndex = 2; ; iRowIndex++)
{
if (null == ((Excel.Range)xSheet.Cells[iRowIndex, 1]).Value2)
{
if ((++iNullRowCount) >= 5)
{
break;
}
continue;
}
strCellText = ((Excel.Range)xSheet.Cells[iRowIndex, 1]).Value2.ToString();
if (!string.IsNullOrEmpty(strCellText))
{
iNullRowCount = 0;
}
else
{
if ((++iNullRowCount) >= 5)
{
break;
}
}
System.Data.DataRow newRow = dtExcel.NewRow();
for (int iColIndex = 1; iColIndex <= iColCount; iColIndex++)
{
if (null == ((Excel.Range)xSheet.Cells[iRowIndex, iColIndex]).Value2)
{
continue;
}
newRow[iColIndex - 1] = ((Excel.Range)xSheet.Cells[iRowIndex, iColIndex]).Value2.ToString();
}
dtExcel.Rows.Add(newRow);
}
xSheet = null;
xBook.Close(Missing.Value, Missing.Value, Missing.Value);
xBook = null;
xApp.Quit();
xApp = null;
return dtExcel;
}
#endregion
#8
//导出EXCEL示例:
Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
excel.Visible = true;
Worksheet sheet = ((Worksheet)excel.ActiveWorkbook.Sheets[1]);
int iRowCount = dataGridView1.Rows.Count;
int iColCount = dataGridView1.ColumnCount;
for (int k = 0; k < iColCount; k++)
{
excel.Cells[1, k + 1] = dataGridView1.Columns[k].HeaderText;
}
for (int i = 0; i < iRowCount; i++)
{
for (int j = 0; j < iColCount; j++)
{
excel.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value;
if ((1 == j) && ("★未匹配" == dataGridView1.Rows[i].Cells[j].Value))
{
sheet.get_Range(sheet.Cells[i + 2, j + 1], sheet.Cells[i + 2, j + 1]).Font.Color = System.Drawing.Color.Blue.ToArgb();
}
}
}
#9
上面的导出代码,不会将EXCEL保存到硬盘,它会打开一个新的EXCEL文件,然后往这个EXCEL文件中一行行写入数据,这个EXCEL文件需要用户自己保存。
#10
不能这样呀,我需要实现的是用户下载Excel,然后对里面的数据进行修改,在导入
报错信息:
System.Data.OleDb.OleDbException: 外部表不是预期的格式。 在 System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) 在 System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) 在 System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) 在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) 在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) 在 System.Data.OleDb.OleDbConnection.Open() 在 Netsource.btn_dr_Click(Object sender, EventArgs e) 位置 e:\InfoNet_zliuy\Netsource.aspx.cs:行号 1092
-------------------------------------------
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();//这里行号为1092
string sql = "select * from [Sheet1$]";
#11
学习一下
#12
protected void btnDownload_Click(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=Sheet1.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write("<table border='1'>");
Response.Write("<tr>");
Response.Write("<td align='center' colspan='4'>注:请注意大小写、全角与半角、-和_、:和;小区名称必须是:460-00-XXXXX-XXXXX</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>网元名</td>");
Response.Write("<td align='center'>基站名</td>");
Response.Write("<td align='center'>小区名</td>");
Response.Write("<td align='center'>是否为工程小区(默认为否)</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC442</td>");
Response.Write("<td align='center'>CDGBSC445:S14:</td>");
Response.Write("<td align='center'>460-00-29542-11441</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC443</td>");
Response.Write("<td align='center'>CDGBSC445:S14:</td>");
Response.Write("<td align='center'>460-00-29542-11442</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S14:</td>");
Response.Write("<td align='center'>460-00-29542-11443</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S22:</td>");
Response.Write("<td align='center'>460-00-29542-10221 </td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S22:</td>");
Response.Write("<td align='center'>460-00-29542-10222 </td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S22:</td>");
Response.Write("<td align='center'>460-00-29542-10223 </td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center' colspan='4'>以上为试例,请从下行可以填写</td>");
Response.Write("</tr>");
string SqlString = "select * from web_site";//查询表
try
{
DataTable dt = dbc.GetDataTable(SqlString);
foreach (DataRow Row in dt.Rows)
{
Response.Write("<tr>");
Response.Write("<td align='center'>" + Row["wy_name"].ToString() + "</td>");
Response.Write("<td align='center'>" + Row["jz_name"].ToString() + "</td>");
Response.Write("<td align='center'>" + Row["xq_name"].ToString() + "</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('"+ex.ToString()+"')</script>");
}
Response.Write("</table>");
Response.End();
}
这个是导出Excel的代码
#13
Mark !
#14
你用我那个7楼的代码导入试试,应该没什么问题。
我给你的是导入导出的代码。
#15
代码看起来蛮复杂的
#16
我在ASP.NET下导入excel到数据库sql2005时也遇到这个问题啦!
#17
匯出的資料是不是Html格式的啊 . 記得把檔案另存為.xls格式噢.....試一下
#18
操作 excel 2007 总是出现问题 好头疼
#19
OleConn.Open();我的程序在这里停了,不报错也不怎样?????
求高手解决!!!!!!!!!
反正xls文件是存在的!!!!!!!
求高手解决!!!!!!!!!
反正xls文件是存在的!!!!!!!