static public string 导出Excel(GridView gv)
{
string FilePath = HttpContext.Current.Request.PhysicalApplicationPath + @"dataResource\TempFile\OutputDataExcel.xlsx";
// ArrayList VisCol = new ArrayList();
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
Microsoft.Office.Interop.Excel.Range ra;
int index_row, index_col;
int jump = 0;
for (index_col = 0; index_col < gv.HeaderRow.Cells.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + 1];
if (gv.HeaderRow.Cells[index_col].Controls.Count > 0 &&
gv.HeaderRow.Cells[index_col].Controls[0] is LinkButton)
ra.set_Value(null,
((LinkButton)gv.HeaderRow.Cells[index_col].Controls[0]).Text
);
else if (gv.HeaderRow.Cells[index_col].Text.Trim() != string.Empty)
ra.set_Value(null,
gv.HeaderRow.Cells[index_col].Text
);
if (gv.HeaderRow.Cells[index_col].Controls.Count > 0 &&
gv.HeaderRow.Cells[index_col].Controls[0] is Table)
{
//写入的位置
//if (index_col + jump + 1 <= ws.Cells.get_End(Microsoft.Office.Interop.Excel.XlDirection.xlToRight).Column)
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + jump + 1];
//读取的位置
Table ta = (Table)gv.HeaderRow.Cells[index_col].Controls[0];
if (index_col <= gv.HeaderRow.Cells.Count - 13 / 2)
{
Microsoft.Office.Interop.Excel.Label la = ta.Rows.Count > 0 && ta.Rows[0].Cells.Count > 0 && ta.Rows[0].Cells[0].Controls.Count > 0 ? (ta.Rows[0].Cells[0].Controls[0] as Microsoft.Office.Interop.Excel.Label) : null;
if (la != null)
ra.set_Value(null,
((Microsoft.Office.Interop.Excel.Label)ta.Rows[0].Cells[0].Controls[0]).Text
);
}
jump++;
}
}
for (index_row = 0; index_row < gv.Rows.Count; index_row++)
{
for (index_col = 0; index_col < gv.Rows[index_row].Cells.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[index_row + 2, index_col + 1];
ra.set_Value(null,
gv.Rows[index_row].Cells[index_col].Text.Replace("¥", "")
//gvr.Cells[index_col].Text.Replace("<br/>", "\n").Trim(" ".ToCharArray()).Trim("#165;".ToCharArray())//"#165;"
/*" "*/
);
//System.Windows.Forms.MessageBox.Show(((Label)gv.Rows[index_row].Cells[index_col].Controls[0]).Text);
//ra.set_Value(null, gv.Rows[index_row].Cells[index_col].Text.Replace("<br/>", "\n").Trim(" ".ToCharArray()).Trim("#165;".ToCharArray()));
}
}
GridViewRow gvr = gv.FooterRow;
for (index_col = 0; index_col < gvr.Cells.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[index_row + 2, index_col + 1];
decimal _Temp;
if (decimal.TryParse(gvr.Cells[index_col].Text.Replace(" ", "").Replace("¥", ""), System.Globalization.NumberStyles.AllowCurrencySymbol, null, out _Temp))
ra.set_Value(null,
_Temp
//gvr.Cells[index_col].Text.Replace("<br/>", "\n").Trim(" ".ToCharArray()).Trim("#165;".ToCharArray())//"#165;"
);
else
ra.set_Value(null, gvr.Cells[index_col].Text.Replace("<br/>", "\n").Trim(" ".ToCharArray()));
}
// ra = ws.UsedRange.get_End(Microsoft.Office.Interop.Excel.XlDirection.xlDown);/*最后一行*/
// ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[ra.Row + 1/*最后一行加一行,也就是新行*/, 3];
// //System.Windows.Forms.MessageBox.Show(gvCon.Rows[85].Cells[3].Text);
// //ra.set_Value(null, "测试的值");
if (System.IO.File.Exists(FilePath)) System.IO.File.Delete(FilePath);
wb.Close(true, FilePath, null);
app.Quit();
string _return = (HttpContext.Current.Request.ApplicationPath == "/" ? string.Empty : HttpContext.Current.Request.ApplicationPath) + "/dataResource/TempFile/OutputDataExcel.xlsx";
GC.Collect();
return _return;
}
{
string FilePath = HttpContext.Current.Request.PhysicalApplicationPath + @"dataResource\TempFile\OutputDataExcel.xlsx";
// ArrayList VisCol = new ArrayList();
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
Microsoft.Office.Interop.Excel.Range ra;
int index_row, index_col;
int jump = 0;
for (index_col = 0; index_col < gv.HeaderRow.Cells.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + 1];
if (gv.HeaderRow.Cells[index_col].Controls.Count > 0 &&
gv.HeaderRow.Cells[index_col].Controls[0] is LinkButton)
ra.set_Value(null,
((LinkButton)gv.HeaderRow.Cells[index_col].Controls[0]).Text
);
else if (gv.HeaderRow.Cells[index_col].Text.Trim() != string.Empty)
ra.set_Value(null,
gv.HeaderRow.Cells[index_col].Text
);
if (gv.HeaderRow.Cells[index_col].Controls.Count > 0 &&
gv.HeaderRow.Cells[index_col].Controls[0] is Table)
{
//写入的位置
//if (index_col + jump + 1 <= ws.Cells.get_End(Microsoft.Office.Interop.Excel.XlDirection.xlToRight).Column)
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + jump + 1];
//读取的位置
Table ta = (Table)gv.HeaderRow.Cells[index_col].Controls[0];
if (index_col <= gv.HeaderRow.Cells.Count - 13 / 2)
{
Microsoft.Office.Interop.Excel.Label la = ta.Rows.Count > 0 && ta.Rows[0].Cells.Count > 0 && ta.Rows[0].Cells[0].Controls.Count > 0 ? (ta.Rows[0].Cells[0].Controls[0] as Microsoft.Office.Interop.Excel.Label) : null;
if (la != null)
ra.set_Value(null,
((Microsoft.Office.Interop.Excel.Label)ta.Rows[0].Cells[0].Controls[0]).Text
);
}
jump++;
}
}
for (index_row = 0; index_row < gv.Rows.Count; index_row++)
{
for (index_col = 0; index_col < gv.Rows[index_row].Cells.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[index_row + 2, index_col + 1];
ra.set_Value(null,
gv.Rows[index_row].Cells[index_col].Text.Replace("¥", "")
//gvr.Cells[index_col].Text.Replace("<br/>", "\n").Trim(" ".ToCharArray()).Trim("#165;".ToCharArray())//"#165;"
/*" "*/
);
//System.Windows.Forms.MessageBox.Show(((Label)gv.Rows[index_row].Cells[index_col].Controls[0]).Text);
//ra.set_Value(null, gv.Rows[index_row].Cells[index_col].Text.Replace("<br/>", "\n").Trim(" ".ToCharArray()).Trim("#165;".ToCharArray()));
}
}
GridViewRow gvr = gv.FooterRow;
for (index_col = 0; index_col < gvr.Cells.Count; index_col++)
{
ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[index_row + 2, index_col + 1];
decimal _Temp;
if (decimal.TryParse(gvr.Cells[index_col].Text.Replace(" ", "").Replace("¥", ""), System.Globalization.NumberStyles.AllowCurrencySymbol, null, out _Temp))
ra.set_Value(null,
_Temp
//gvr.Cells[index_col].Text.Replace("<br/>", "\n").Trim(" ".ToCharArray()).Trim("#165;".ToCharArray())//"#165;"
);
else
ra.set_Value(null, gvr.Cells[index_col].Text.Replace("<br/>", "\n").Trim(" ".ToCharArray()));
}
// ra = ws.UsedRange.get_End(Microsoft.Office.Interop.Excel.XlDirection.xlDown);/*最后一行*/
// ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[ra.Row + 1/*最后一行加一行,也就是新行*/, 3];
// //System.Windows.Forms.MessageBox.Show(gvCon.Rows[85].Cells[3].Text);
// //ra.set_Value(null, "测试的值");
if (System.IO.File.Exists(FilePath)) System.IO.File.Delete(FilePath);
wb.Close(true, FilePath, null);
app.Quit();
string _return = (HttpContext.Current.Request.ApplicationPath == "/" ? string.Empty : HttpContext.Current.Request.ApplicationPath) + "/dataResource/TempFile/OutputDataExcel.xlsx";
GC.Collect();
return _return;
}