C#导出用户自定义的excel 用户建立一个excel (excel有它的样式)然后我们按照这个excel模板导出数据有人做过这个样的东西吗 很难啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.AllowPaging = false; GridView1.DataBind(); //Change the Header Row back to white color GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF"); //Apply style to Individual Cells GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green"); GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green"); GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green"); GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green"); for (int i = 0; i < GridView1.Rows.Count;i++ ) { GridViewRow row = GridView1.Rows[i]; 不是这样的 你这个是自己设置格式 我需要的是用户自己设置样式 然后按照用户设置的样式导出excel 那你得先弄懂导出Excel原理,然后才能做"按照用户设置的样式导出"楼主还是先弄懂一个简单的导出例子吧.这个问题,别人没办法给你代码,只能给例子 简单导出我已经弄好了 现在需要的是用户自定义导出 就是用户给模板 我找到思路了 /// <summary> /// 把table中的数据导出到excel中去 /// </summary> /// <param name="dt">要导入的table</param> /// <param name="maxcount">模板excel中,一个sheet要显示的行数,如果数据多一个sheet中的最大值,会自动生成新的sheet</param> private void ExportExcel(System .Data .DataTable dt,int maxcount) { Random ran = new Random(); string fileautoname = Server.MapPath("~/") + "Files\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(100, 999) + ".xls";//给新文件命名 string filepath = Server.MapPath("~/") + "EXCEL_Template\\Excel\\模板.xls";//模板文件路径 object missing = Type.Missing; Microsoft.Office.Interop.Excel.Application application = new Application(); Workbook workbook = application.Workbooks.Open(filepath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); Worksheet worksheet; worksheet = (Worksheet)workbook.Sheets.get_Item(1); DataView dv = dt.DefaultView; dv.Sort = "id asc"; //table 中的数据按id升序排列 System.Data.DataTable table = dv.ToTable(); int sheetcount = GetSheetCount(table.Rows.Count, maxcount); for (int count = 1; count < sheetcount; count++) { ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(count)).Copy(missing, workbook.Worksheets[count]); } List<object[,]> list = new List<object[,]>(); object[,] ret; for (int count = 0; count < sheetcount; count++) { if (count == sheetcount - 1) { ret = new object[table.Rows.Count - count * maxcount, table.Columns.Count - 2]; for (int i = 0; i < table.Rows.Count - count * maxcount; i++) { for (int j = 0; j < table.Columns.Count - 2; j++) { ret[i, j] = table.Rows[count * maxcount + 1][j]; } } list.Add(ret); } else { ret = new object[maxcount, table.Columns.Count - 2]; for (int i = 0; i < maxcount; i++) { for (int j = 0; j < table.Columns.Count - 2; j++) { ret[i, j] = table.Rows[i + count * maxcount][j]; } } list.Add(ret); } } object[,] obj; for (int p = 0; p < list.Count; p++) { worksheet = (Worksheet)workbook.Sheets.get_Item(p + 1); obj = list[p]; string cn = "L" + (obj.GetLength(0) + 2).ToString(); //设置填充区域 worksheet.get_Range("A3", cn).FormulaR1C1 = obj; } workbook.SaveAs(fileautoname, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); workbook.Close(missing, missing, missing); application.Quit(); workbook = null; } /// <summary> /// 获取WorkSheet数量 /// </summary> /// <param name="rowCount">记录总行数</param> /// <param name="rows">每WorkSheet行数</param> /// <returns></returns> private int GetSheetCount(int rowCount,int rows) { int n = rowCount % rows; //余数 if(n == 0) return rowCount / rows; else return Convert.ToInt32(rowCount / rows) + 1; } 打开excel模板,通过二维数组赋值给单元格 这种性能不是很好,记得CodeProject里有一个C#实现读写Excel二进制(BIFF8)格式的例子,非常好。可以去搜索一下。 API 录音函数的device参数 请问一个datagridview的优化问题 求助关于一个简单的小程序 ACCESS 能新增记录但不能删除记录的问题!急 菜鸟问题 大家答 请问用Enter取代Tab焦点切换效果的代码怎么写? "派生类实例化时要首先要执行基类的静态构造函数"这句话对吗? string remove()不起作用 请教,用2进制方式读取一个文件,并且换算为它的十六进制方式,保存到另一个文件。 C#:“添加Web引用”不能成功,请急救!!! C#中(非MDI)导航菜单、窗体、实例化在一个窗体加载时关闭其他所有打开的窗体, 小小菜遇到一个程序设计的问题烦啊,请各位大侠来帮帮忙
楼主还是先弄懂一个简单的导出例子吧.
这个问题,别人没办法给你代码,只能给例子
我找到思路了
/// <summary>
/// 把table中的数据导出到excel中去
/// </summary>
/// <param name="dt">要导入的table</param>
/// <param name="maxcount">模板excel中,一个sheet要显示的行数,如果数据多一个sheet中的最大值,会自动生成新的sheet</param>
private void ExportExcel(System .Data .DataTable dt,int maxcount)
{
Random ran = new Random();
string fileautoname = Server.MapPath("~/") + "Files\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(100, 999) + ".xls";//给新文件命名
string filepath = Server.MapPath("~/") + "EXCEL_Template\\Excel\\模板.xls";//模板文件路径
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Application application = new Application();
Workbook workbook = application.Workbooks.Open(filepath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Worksheet worksheet;
worksheet = (Worksheet)workbook.Sheets.get_Item(1);
DataView dv = dt.DefaultView;
dv.Sort = "id asc"; //table 中的数据按id升序排列
System.Data.DataTable table = dv.ToTable();
int sheetcount = GetSheetCount(table.Rows.Count, maxcount);
for (int count = 1; count < sheetcount; count++)
{
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(count)).Copy(missing, workbook.Worksheets[count]);
}
List<object[,]> list = new List<object[,]>();
object[,] ret;
for (int count = 0; count < sheetcount; count++)
{
if (count == sheetcount - 1)
{
ret = new object[table.Rows.Count - count * maxcount, table.Columns.Count - 2];
for (int i = 0; i < table.Rows.Count - count * maxcount; i++)
{
for (int j = 0; j < table.Columns.Count - 2; j++)
{
ret[i, j] = table.Rows[count * maxcount + 1][j];
}
}
list.Add(ret);
}
else
{
ret = new object[maxcount, table.Columns.Count - 2];
for (int i = 0; i < maxcount; i++)
{
for (int j = 0; j < table.Columns.Count - 2; j++)
{
ret[i, j] = table.Rows[i + count * maxcount][j];
}
}
list.Add(ret);
}
} object[,] obj;
for (int p = 0; p < list.Count; p++)
{
worksheet = (Worksheet)workbook.Sheets.get_Item(p + 1);
obj = list[p];
string cn = "L" + (obj.GetLength(0) + 2).ToString(); //设置填充区域
worksheet.get_Range("A3", cn).FormulaR1C1 = obj;
}
workbook.SaveAs(fileautoname, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
workbook.Close(missing, missing, missing);
application.Quit();
workbook = null;
}
/// <summary>
/// 获取WorkSheet数量
/// </summary>
/// <param name="rowCount">记录总行数</param>
/// <param name="rows">每WorkSheet行数</param>
/// <returns></returns>
private int GetSheetCount(int rowCount,int rows)
{
int n = rowCount % rows; //余数 if(n == 0)
return rowCount / rows;
else
return Convert.ToInt32(rowCount / rows) + 1;
}