c# excel 2

① 怎样把DataGrid的数据导出到Excel以供打印?

  ② 之前已经为DataGrid设置了TableStyle,即自定义了列标题和要显示的列,如果想以自定义的视图导出数据该怎么办?

  ③ 把数据导出到Excel后,怎样为它设置边框啊?

  ④ 怎样使从DataGrid导出到Excel的某个列居中对齐?

  ⑤ 数据从DataGrid导出到Excel后,怎样使标题行在打印时出现在每一页?

  ⑥ DataGrid数据导出到Excel后打印时每一页显示’当前页/共几页’,怎样实现?

  ①

  private void button1_Click(object sender, System.EventArgs e)

  {

  int row_index, col_index; 

  

  row_index = 1; 

  col_index = 1; 

  

  Excel.ApplicationClass excel = new Excel.ApplicationClass(); 

  excel.Workbooks.Add(true); 

  

  DataTable dt = ds.Tables["table"]; 

  

  foreach(DataColumn dcHeader in dt.Columns)

  excel.Cells[row_index, col_index++] = dcHeader.ColumnName; 

  

  foreach(DataRow dr in dt.Rows)

  {

  col_index = 0; 

  foreach(DataColumn dc in dt.Columns)

  {

  excel.Cells[row_index+1, col_index+1] = dr[dc]; 

  col_index++; 

  }

  row_index++; 

  }

  excel.Visible = true; 

  

  }

  

  private void Form1_Load(object sender, System.EventArgs e)

  {

  SqlConnection conn = new SqlConnection("server=tao; uid=sa; pwd=; database=pubs"); 

  conn.Open(); 

  

  SqlDataAdapter da = new SqlDataAdapter("select * from authors", conn); 

  ds = new DataSet(); 

  da.Fill(ds, "table"); 

  

  dataGrid1.DataSource = ds; 

  dataGrid1.DataMember = "table"; 

  }

  ②dataGrid1.TableStyles[0].GridColumnStyles[index].HeaderText; //index可以从0~dataGrid1.TableStyles[0].GridColumnStyles.Count遍历。

  ③ Excel.Range range; 

  range=worksheet.get_Range(worksheet.Cells[1,1],xSt.Cells[ds.Tables[0].Rows.Count+1,ds.Tables[0].Columns.Count]); 

  

  range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null); 

  

  range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; 

  range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous; 

  range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin; 

  

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic; 

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous; 

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin; 

  ④ range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter

  ⑤ worksheet.PageSetup.PrintTitleRows = "$1:$1"; 

  ⑥ worksheet.PageSetup.CenterFooter = "第&P页 / 共&N页"; 

  22.当把DataGrid的Cell内容赋值到Excel的过程中想在DataGrid的CaptionText上显示进度,但不显示。WHY?

  ...

  dataGrid1.CaptionText = "正在导出:" + (row + 1) + "/" + row_cnt; 

  System.Windows.Forms.Application.DoEvents(); 

 

 

 

 

C#通过OLEDB读取Excel文件

这是一个最简单的打开Excel 读取程序。

private void button1_Click(object sender, EventArgs e)
{

//首先是给出文件名
string fileName = "A.xlsx";
//string fileName = "B.xls";

//第二步,设定链接字符串
string connectString = GetConnectionString(fileName);

//第三步,建立连接
OleDbConnection conn = new OleDbConnection(connectString);

//第四步,打开链接
conn.Open();

//第五步,设定Select命令,这里表的名字是Sheet1,就是在Excel中下方的Tab名字。在加上一个$符号。
string cmdText = "select * from [Sheet1$]";
OleDbCommand comm = new OleDbCommand(cmdText, conn);

//第六步,构建DataAdapter
OleDbDataAdapter da = new OleDbDataAdapter(comm);

//第七步,填充数据
DataSet allData = new DataSet();
da.Fill(allData);

//第八步,结束
conn.Close();

//读出的数据就是一个二维的表,和表单里头呈现的一样。按照行列来读,不需要考虑其他因素

//可以修改,插入数据,但是不可以删除。这些可以通过设定DataAdapter的Command来实现。插入修改后会保留原有格式。
if (allData.Tables.Count > 0)
{
int rowCount = allData.Tables[0].Rows.Count;
int colCount = allData.Tables[0].Columns.Count;
for (int i = 0; i < colCount; i++)
{
string s1 = allData.Tables[0].Columns[i].ColumnName;

}
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
string s1 = allData.Tables[0].Rows[i][j].ToString();
}
}
}


}

//这是给出了两个连接字符串,一个是xls,第二个是xlsx,就是Excel 2007格式。如果需要更多的字符串,可以访问这个网站找答案。Privider就是OLEDB的提供者,这个很常见。DataSource就是文件名,

ExtendedProperties:

 一个选项是Excel的版本号,

一个选项是HDR=YES/No; 就是第一行是不是数据的列名,如果是就自动用这个做Table的列名。如果不是就用A1,A2,F3这些名字。

一个选项是IMEX=1 就是说数据是不是混排的,叫Intermixed。这样读取时候直接按Text读,不自行根据第一列的数据属性瞎猜。否则,如果第一行是数字,会默认以后这一列全是数字,如果不是数字呢,就给个空值。

http://www.connectionstrings.com/?carrier=excel2007

//如果连接字符串不对,有可能出现"Could not find installable ISAM ” Exception
private string GetConnectionString(string fileName)
{
//xls文件的连接字符串
/*string connectString =
@" Provider=Microsoft.Jet.OLEDB.4.0;" +
@" Data Source=" + fileName + ";" +
@" Extended Properties=" + Convert.ToChar(34).ToString() +
@" Excel 8.0;" + Convert.ToChar(34).ToString();*/
//xlsx,Excel 2007文件的连接字符串
string connectString =
@" Provider=Microsoft.ACE.OLEDB.12.0;" +
@" Data Source=" + fileName + ";" +
@" Extended Properties=" + Convert.ToChar(34).ToString() +
@" Excel 12.0;" + Convert.ToChar(34).ToString();

return connectString;

}

 

http://www.connectionstrings.com/excel

posted on 2009-03-09 00:47  hcmfys_lover  阅读(234)  评论(0编辑  收藏  举报