这是我的代码:
try
{
DataSet Ds = new DataSet();
Ds.ReadXml(System.AppDomain.CurrentDomain.BaseDirectory + @"\DataSet.xml");
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel._Workbook xBk = excel.Workbooks.Add(true);
Microsoft.Office.Interop.Excel._Worksheet xSt = (Microsoft.Office.Interop.Excel._Worksheet)xBk.ActiveSheet;
Microsoft.Office.Interop.Excel.Range excelCell = null;
try
{
DataTable Dt = Ds.Tables[1];
int RowLoc;
for (int i = 0; i < Dt.Columns.Count; ++i)
{
string tmp = Dt.Rows[0][i].ToString();
RowLoc = 0;
for (int j = 0; j < Dt.Rows.Count; ++j)
{
if (tmp == Dt.Rows[j][i].ToString())
{
if (RowLoc < j)
{
for (int CurrCol = 0; CurrCol < i; ++CurrCol)
{
if (Dt.Rows[j - 1][CurrCol].ToString() != Dt.Rows[j][CurrCol].ToString())
{
tmp = Dt.Rows[j][i].ToString();
RowLoc = j;
xSt.Cells[j + 1, i + 1] = tmp;
break;
}
}
excelCell = xSt.get_Range(excel.Cells[RowLoc + 1, i + 1], excel.Cells[j+1, i + 1]);
excelCell.Select();
excelCell.Merge(false);
excelCell.Value2 = tmp;
tmp = Dt.Rows[RowLoc][i].ToString();
RowLoc = j;
}
else
{
tmp = Dt.Rows[j][i].ToString();
RowLoc = j;
xSt.Cells[j + 1, i + 1] = tmp;
}
}
else
{
tmp = Dt.Rows[j][i].ToString();
RowLoc = j;
xSt.Cells[j + 1, i + 1] = tmp;
}
}
}
excel.Visible = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
excelCell = null;
xBk = null;
xSt = null;
excel = null;
GC.Collect();
}
}
catch (Exception ex)
{
throw ex;
}
try
{
DataSet Ds = new DataSet();
Ds.ReadXml(System.AppDomain.CurrentDomain.BaseDirectory + @"\DataSet.xml");
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel._Workbook xBk = excel.Workbooks.Add(true);
Microsoft.Office.Interop.Excel._Worksheet xSt = (Microsoft.Office.Interop.Excel._Worksheet)xBk.ActiveSheet;
Microsoft.Office.Interop.Excel.Range excelCell = null;
try
{
DataTable Dt = Ds.Tables[1];
int RowLoc;
for (int i = 0; i < Dt.Columns.Count; ++i)
{
string tmp = Dt.Rows[0][i].ToString();
RowLoc = 0;
for (int j = 0; j < Dt.Rows.Count; ++j)
{
if (tmp == Dt.Rows[j][i].ToString())
{
if (RowLoc < j)
{
for (int CurrCol = 0; CurrCol < i; ++CurrCol)
{
if (Dt.Rows[j - 1][CurrCol].ToString() != Dt.Rows[j][CurrCol].ToString())
{
tmp = Dt.Rows[j][i].ToString();
RowLoc = j;
xSt.Cells[j + 1, i + 1] = tmp;
break;
}
}
excelCell = xSt.get_Range(excel.Cells[RowLoc + 1, i + 1], excel.Cells[j+1, i + 1]);
excelCell.Select();
excelCell.Merge(false);
excelCell.Value2 = tmp;
tmp = Dt.Rows[RowLoc][i].ToString();
RowLoc = j;
}
else
{
tmp = Dt.Rows[j][i].ToString();
RowLoc = j;
xSt.Cells[j + 1, i + 1] = tmp;
}
}
else
{
tmp = Dt.Rows[j][i].ToString();
RowLoc = j;
xSt.Cells[j + 1, i + 1] = tmp;
}
}
}
excel.Visible = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
excelCell = null;
xBk = null;
xSt = null;
excel = null;
GC.Collect();
}
}
catch (Exception ex)
{
throw ex;
}
环境:windows2003 + excel 2007 + IIS6.0
首先没有使用模拟
找到组件服务里面的excel组件,选属性,进入安全页里面
将“启动和激活权限”改为自定义,并添加network service (如果是IIS5 则为aspnet)
将“访问权限”改为自定义,并添加network service (如果是IIS5 则为aspnet)
你试试看
http://support.microsoft.com/default.aspx?scid=kb;EN-US;257757
谢谢大家的关注
谢谢你的提示,我刚刚按照步骤作了一下,结果依然。我想问问 “首先没有使用模拟” 是什么意思?不太明白。 呵呵
如果还不行,建议换个机器和其他版本的EXCEL试试
xl.DefaultSaveFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8;
var oBook = xl.Workbooks.Add();