大家看下我的完整代码吧:
public void daochu_all(DataSet exl_target, DataSet exl_workRTC, DataSet exl_workType, DataSet exl_rateAdd, DataSet exl_ratePhase, DataSet exl_bugRemove, DataSet exl_reviewSpeed, string path)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlBook = null;
string fullpath = path.Substring(0, path.Length - 4) + "fileupload ";
string templatepath = fullpath + "\\BaselineTemplate ";
string productpath = fullpath + "\\BaselineProduct ";
string userpath = templatepath + "\\excel.xls ";

DirectoryInfo dtemplate = new DirectoryInfo(templatepath);
DirectoryInfo dproduct = new DirectoryInfo(productpath);

if (dtemplate.Exists)
{
}
else
{
Directory.CreateDirectory(templatepath);
}

if (dproduct.Exists)
{
}
else
{
Directory.CreateDirectory(productpath);
}


if (File.Exists(userpath))
{
try
{
xlBook = xlApp.Workbooks.Add(userpath);
//xlSheet = (Excel.Worksheet)_Item( "项目性能查询 (性能基线表) ");

System.Data.DataTable Table_target = exl_target.Tables[0];
System.Data.DataTable Table_workRTC = exl_workRTC.Tables[0];
System.Data.DataTable Table_workType = exl_workType.Tables[0];
System.Data.DataTable Table_rateAdd = exl_rateAdd.Tables[0];
System.Data.DataTable Table_ratePhase = exl_ratePhase.Tables[0];
System.Data.DataTable Table_bugRemove = exl_bugRemove.Tables[0];
System.Data.DataTable Table_reviewSpeed = exl_reviewSpeed.Tables[0];
double a = Convert.ToDouble(Table_target.Rows[0][2]);
double a1 = Convert.ToDouble(Table_target.Rows[0][3]);
double a2 = Convert.ToDouble(Table_target.Rows[0][4]);

double a3 = Convert.ToDouble(Table_target.Rows[1][2]);
double a4 = Convert.ToDouble(Table_target.Rows[1][3]);
double a5 = Convert.ToDouble(Table_target.Rows[1][4]);

//--------------------整体性能-------------------

int Tablerow = 5;


for (int i = 0; i < Table_target.Rows.Count; i++)//取记录值
{
string tmpName = Table_target.Rows[i][0].ToString();

for (int j = 0; j < (Table_target.Columns.Count - 2); j++)
{

if (tmpName == "1 " || tmpName == "2 " || tmpName == "5 ")
{
xlApp.Cells[Tablerow, j + 2] = Convert.ToDouble((Table_target.Rows[i][j + 2].ToString())) / 100;
}
else
{
xlApp.Cells[Tablerow, j + 2] = Table_target.Rows[i][j + 2].ToString();
}


}
Tablerow++;
}

//--------------------工作量比重-相对于编码-------------------

int Table2row = 16;

for (int i = 0; i < Table_workRTC.Rows.Count; i++)//取记录值
{

for (int j = 0; j < (Table_workRTC.Columns.Count - 2); j++)
{

xlApp.Cells[Table2row, j + 2] = Table_workRTC.Rows[i][j + 2].ToString();

}
Table2row++;
}

//--------------------各类工作比例-------------------

int Table3row = 32;

for (int i = 0; i < Table_workType.Rows.Count; i++)//取记录值
{

for (int j = 0; j < (Table_workType.Columns.Count - 2); j++)
{


xlApp.Cells[Table3row, j + 2] = Convert.ToDouble((Table_workType.Rows[i][j + 2].ToString())) / 100;

}
Table3row++;
}

//--------------------缺陷排除率-累计-------------------

int Table4row = 56;

for (int i = 0; i < Table_rateAdd.Rows.Count; i++)//取记录值
{

for (int j = 0; j < (Table_rateAdd.Columns.Count - 2); j++)
{

xlApp.Cells[Table4row, j + 2] = Convert.ToDouble((Table_rateAdd.Rows[i][j + 2].ToString())) / 100;

}
Table4row++; tabletable
}

//--------------------缺陷排除率-当前阶段-------------------

int Table5row = 71;

for (int i = 0; i < Table_ratePhase.Rows.Count; i++)//取记录值
{

for (int j = 0; j < (Table_ratePhase.Columns.Count - 2); j++)
{

xlApp.Cells[Table5row, j + 2] = Convert.ToDouble((Table_ratePhase.Rows[i][j + 2].ToString())) / 100;

}
Table5row++;
}

//--------------------缺陷排除密度-------------------

int Table6row = 84;


for (int i = 0; i < Table_bugRemove.Rows.Count; i++)//取记录值
{

for (int j = 0; j < (Table_bugRemove.Columns.Count - 2); j++)
{

xlApp.Cells[Table6row, j + 2] = Table_bugRemove.Rows[i][j + 2].ToString();

}
Table6row++;
}

//--------------------评审速度-------------------

int Table7row = 99;

for (int i = 0; i < Table_reviewSpeed.Rows.Count; i++)//取记录值
{

for (int j = 0; j < (Table_reviewSpeed.Columns.Count - 2); j++)
{

xlApp.Cells[Table7row, j + 2] = Table_reviewSpeed.Rows[i][j + 2].ToString();

}
Table7row++;
}

xlApp.Application.DisplayAlerts = false;
xlBook.SaveAs(productpath + "\\BaselineProduct.xls ", xlApp.ActiveWorkbook.FileFormat, " ", " ", xlApp.ActiveWorkbook.ReadOnlyRecommended, xlApp.ActiveWorkbook.CreateBackup, XlSaveAsAccessMode.xlNoChange, xlApp.ActiveWorkbook.ConflictResolution, false, " ", " ", " ");
}
catch
{
throw new Exception( "生成性能基线所需模板或数据库中数据有误 ");
}
finally
{
Process[] processes = Process.GetProcesses;
Process process;
foreach (process in processes)
{


}

if (xlBook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
xlBook = null;
}
if (xlApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp.Quit();
xlApp = null;
}
GC.Collect();
}
}
else
{
throw new Exception( "服务器BaselineTemplate中未发现模板文件 ");
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。