如何将datatable中的值导出到指定的excel模板中
1个回答
展开全部
/// <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 + i][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;
}
/// 把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 + i][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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |