110,539
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 导出excel方法
/// </summary>
/// <param name="title"></param>
/// <param name="exprotGrid"></param>
/// <returns></returns>
public static string exprotExcel(string title, DataGridView exprotGrid)
{
string message = "";
if (exprotGrid.Rows.Count == 0)
{
message = "当前数据为空,不执行导出";
return message;
}
Excel.Application exlapp = new Excel.Application();
if (exlapp == null)
{
message = "excel无法启动";
return message;
}
Excel.Workbook exlbook = exlapp.Workbooks.Add(true);
Excel.Worksheet exlsheet = (Excel.Worksheet)exlbook.Worksheets[1];
Excel.Range range = exlsheet.get_Range(exlapp.Cells[1, 1], exlapp.Cells[1, exprotGrid.ColumnCount]);
range.MergeCells = true;
exlapp.ActiveCell.FormulaR1C1 = title;
exlapp.ActiveCell.Font.Size = 20;
exlapp.ActiveCell.Font.Bold = true;
exlapp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
int colIndex = 0;
int rowIndex = 0;
int colCount = exprotGrid.ColumnCount;
int rowCount = exprotGrid.RowCount;
object[,] objdata = new object[rowCount + 1, colCount];
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objdata[rowIndex, colIndex] = exprotGrid.Columns[colIndex].HeaderText;
}
for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
if (exprotGrid[colIndex, rowIndex].Value != null)
{
if (exprotGrid[colIndex, rowIndex].Value.ToString().ToLower() == "true")
{
objdata[rowIndex + 1, colIndex] = "是";
}
else if (exprotGrid[colIndex, rowIndex].Value.ToString().ToLower() == "false")
{
objdata[rowIndex + 1, colIndex] = "否";
}
else
{
objdata[rowIndex + 1, colIndex] = exprotGrid[colIndex, rowIndex].Value.ToString();
}
}
else
{
objdata[rowIndex + 1, colIndex] = "";
}
}
Application.DoEvents();
}
exlapp.get_Range(exlapp.Cells[2, 1], exlapp.Cells[2, colIndex]).Font.Bold = true;
range = exlsheet.get_Range(exlapp.Cells[2, 1], exlapp.Cells[rowCount + 2, colCount]);
range.Value2 = objdata;
try
{
exlapp.Cells.EntireColumn.AutoFit();
exlapp.Cells.VerticalAlignment = Excel.Constants.xlCenter;
exlapp.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
exlapp.Visible = true;
}
catch
{
message = "保存出错,请检查";
return message;
}
return message;
}
//Web的
protected void btn_ToExcel_Click(object sender, EventArgs e)
{
Export(this.gv_Contract, "application/ms-excel", string.Format("合同列表_{0}.xls", Session["UserName"].ToString()));
}
private void Export(GridView gv, string filetype, string filename)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", HttpUtility.UrlEncode(filename)));
Response.ContentType = filetype;
EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ClearControls(gv);
gv.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{ }
private void ClearControls(GridView gv)
{
for (int i = 0; i < gv.Rows.Count; i++)
{
gv.Rows[i].Cells[30].Controls.Clear();
}
gv.Columns[30].Visible = false;
}
//WinForm的
#region 将DataGridView控件中数据导出到Excel
/// <summary>
/// 将DataGridView控件中数据导出到Excel
/// </summary>
/// <param name="gridView">DataGridView对象</param>
/// <param name="isShowExcle">是否显示Excel界面</param>
/// <returns></returns>
public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
{
if (gridView.Rows.Count == 0)
return false;
//建立Excel对象
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成字段名称
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
}
return true;
}
#endregion