乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 如何将datatable中的值导出到指定的excel模板中-excel导入datatable,excel读入datata

如何将datatable中的值导出到指定的excel模板中-excel导入datatable,excel读入datata

作者:乔山办公网日期:

返回目录:excel表格制作


下面的函数作用,将DataTable导出到EXCEL文件:
private void DataTabletoExcel(System.Data.DataTable tmpDataTable,string strFileName)
{
if (tmpDataTable == null)
{
return;
}
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;

Excel.Application xlApp = new Excel.ApplicationClass();

xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;

Excel.Workbook xlBook = xlApp.Workbooks.Add(true);

//将DataTable的列名导入e5a48de588b67a64330Excel表第一行
foreach(DataColumn dc in tmpDataTable.Columns)
{
columnIndex ++;
xlApp.Cells[rowIndex,columnIndex] = dc.ColumnName;
}

//将DataTable中的数据导入Excel中
for(int i = 0;i<rowNum; i++)
{
rowIndex ++;
columnIndex = 0;
for (int j = 0;j<columnNum; j++)
{
columnIndex ++;
xlApp.Cells[rowIndex,columnIndex] = tmpDataTable.Rows[i][j].ToString();
}
}
xlBook.SaveCopyAs(strFileName + ".xls");
}

/// <summary>
/// 把table中的数据导出到excel中去
/// </summary>
/// <param name="dt">要导入的e69da5e6ba907a686964616f365table</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;
}
atlab怎么样将矩阵或数组中国的信息全部导出呢?因为复制实在是太麻烦了。很简单的用xlswrite函数就可以了。首先打开matlab,输入你的代码,

然后找zhidao到你要存放文件的位置复制绝对路径(致谢文件名的话就会存放在当前目录中),如图以f盘根目录为例)

然后写xlswrite函数,具体格式课参看help xlswrite,
xlswrite函数前一项是路径,后一项是要输出的矩阵。

然后点击执行,等待一下,就会在指定位置新建xls文件,

如要竖着输出数据的话,将矩阵转置即可
在后面可控制输出数据在xls中的范围,如A:D,2:16等
注意:转置后原来的数据只会被覆盖不会消失
要读出xls中的文件时可用函数xlsread用法相同。

using Microsoft.Office.Core;using Microsoft.Office.Interop.Excel;Microsoft.Office.Interop.Excel.Application myExcel=new Microsoft.Office.Interop.Excel.Application();myExcel.Application.Workbooks.Add(true);for(int row=0;row<=this.ds.Tables[0].Rows.Count-1;row++){for(int col=0;col<=this.ds.Tables[0].Columns.Count-1;col++){myExcel.Cells[row+4,col+1]="'"+ds.Tables[0].Rows[row].ItemArray[col].ToString();}}这个e799bee5baa6e997aee7ad94e78988e69d83331是把datatable上的数据直接绑定到datagird上面,然后再从这个上面下载到excel上.private void btnDownLoad_Click(object sender, System.EventArgs e){if(this.dg.Items.Count0){Response.Clear();Response.Buffer= true;Response.Charset="GB2312";Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网