作者:乔山办公网日期:
返回目录:excel表格制作
========== 方法一
DataTable tblDatas = newDataTable("Datas");
DataColumn dc = null;
//赋值给dc,是便于对每一个datacolumn的操作
dc =tblDatas.Columns.Add("ID",Type.GetType("System.Int32"));
dc.AutoIncrement= true;//自动增加
dc.AutoIncrementSeed = 1;//起始为1
dc.AutoIncrementStep = 1;//步长为1
dc.AllowDBNull = false;//
dc = tblDatas.Columns.Add("Product",Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Version",Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Description",Type.GetType("System.String"));
DataRow newRow;
newRow = tblDatas.NewRow();
newRow["Product"] = "大话西游";
newRow["Version"] = "2.0";
newRow["Description"] = "我很喜欢";
tblDatas.Rows.Add(newRow);
newRow = tblDatas.NewRow();
newRow["Product"] = "梦幻西游";
newRow["Version"] = "3.0";
newRow["Description"] = "比大话更幼稚";
tblDatas.Rows.Add(newRow);
========== 方法二
DataTable tblDatas = newDataTable("Datas");
tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
tblDatas.Columns[0].AutoIncrement = true;
tblDatas.Columns[0].AutoIncrementSeed = 1;
tblDatas.Columns[0].AutoIncrementStep = 1;
tblDatas.Columns.Add("Product",Type.GetType("System.String"));
tblDatas.Columns.Add("Version",Type.GetType("System.String"));
tblDatas.Columns.Add("Description",Type.GetType("System.String"));
tblDatas.Rows.Add(newobject[]{null,"a","b","c"});
tblDatas.Rows.Add(newobject[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
========== 方法三
DataTable table = new DataTable ();
//创建table的第一列
DataColumn priceColumn = new DataColumn();
//该列的数据类型
priceColumn.DataType = System.Type.GetType("System.Decimal");
//该列得名称
priceColumn.ColumnName = "price";
//该列得默认值
priceColumn.DefaultValue =50;
// 创建table的第二列
DataColumn taxColumn = new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
//列名
taxColumn.ColumnName = "tax";
//设置该列得表达式,用于计算列中的值或创建聚合列
taxColumn.expression_r_r = "price *0.0862";
// Create third column.
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName = "total";
//该列的表达式,值是得到的是第一列和第二列值得和
totalColumn.expression_r_r = "price + tax";
// 将所有的e69da5e887aa7a64336列添加到table上
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);
//创建一行
DataRow row = table.NewRow();
//将此行添加到table中
table.Rows.Add(row);
//将table放在试图中
DataViewview = new DataView(table);
dg.DataSource = view;
dg.DataBind();
private void AddDataToExcel(System.Data.DataTable dt, string filename)
{
try
{
//write
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excelApplication.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
//excelApplication.Visible = true;
#region//填充数e799bee5baa6e58685e5aeb9362据
for (int i = 0; i < dt.Columns.Count - 1; i++)
{
for (int j = 0; j < dt.Rows.Count - 1; j++)
{
excelApplication.Cells[i + 1, j + 1] = dt.Rows[i][j];
//Value.ToString();
}
}
#endregion
//workbook.SaveCopyAs (filepathoutput +"\\"+ strB + ".xlsx");
worksheet.SaveAs(filename + ".xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
worksheet = null;
workbook.Close(false, Missing.Value, Missing.Value);
excelApplication.Quit();
excelApplication = null;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//添加按钮列
private void AddBtnColumn()
{
DataGridViewButtonColumn colBtn = new DataGridViewButtonColumn();
colBtn.UseColumnTextForButtonValue = true;
colBtn.Text = "选中";
dataGridView1.Columns.Add(colBtn);
}
//新增事件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.Equals(1))//定位到按钮列
{
MessageBox.Show("选中的值为e799bee5baa6e58685e5aeb9339:"+dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
连接字百符串里面有相关的度关键字选问项,如下答(07版)版 Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls; Extended Properties="Excel 8.0;HDR=YES"; HDR=YES就代表第一行是标题权。