关于Excel表的问题~~ gradview用数据源绑定的Excel表能否在后台直接用SQL语句对表进行操作啊~~~数据源绑定和代码绑定的Excel表有啥区别啊~~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我是菜鸟,ACCESS倒是有用作过DB,EXCEL想用但目前不会。 第一种:Microsoft.Office.Interop.Excel.Application appExcel = null;//创建EXCEL对像 Microsoft.Office.Interop.Excel.Workbook WorkBook;//创建工作薄 Microsoft.Office.Interop.Excel.Worksheet ws = null;//创建工作表 private void UserControl1_Load(object sender, EventArgs e) { for (int i = 1; i <= 256; i++) { cmbSave.Items.Add(i.ToString()); } } private void btnLL_Click(object sender, EventArgs e) { openFileDialog1.Filter = "(*.xls)|*.xls";//对话框只读取EXCEL文件夹 openFileDialog1.FileName = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK) { txtFileName.Text = openFileDialog1.FileName; try { cmbWorksheet.Items.Clear(); appExcel = new Microsoft.Office.Interop.Excel.Application();//实例化EXCEL对像 WorkBook = appExcel.Workbooks.Open(txtFileName.Text, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//实例化工作薄 #region 将工作表的名称读到cmbworksheet中 cmbWorksheet.Items.Add("请选择"); for (int i = 1; i <= WorkBook.Worksheets.Count; i++) { ws = (Microsoft.Office.Interop.Excel.Worksheet)WorkBook.Worksheets[i]; cmbWorksheet.Items.Add(ws.Name.ToString()); } cmbWorksheet.SelectedIndex = 0; #endregion } catch { } finally { appExcel.Quit(); appExcel = null; } } } private void cmbWorksheet_SelectedIndexChanged(object sender, EventArgs e) { cmbWorkli.Items.Clear(); if (cmbWorksheet.Text == "请选择") { return; } try { appExcel = new Microsoft.Office.Interop.Excel.Application();//实例化EXCEL对像 WorkBook = appExcel.Workbooks.Open(txtFileName.Text, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//实例化工作薄 //获取工作表 ws = (Microsoft.Office.Interop.Excel.Worksheet)WorkBook.Worksheets[int.Parse(cmbWorksheet.SelectedIndex.ToString())]; #region 读取工作表中的列头到cmbWorkli控件中 cmbWorkli.Items.Add("请选择字段"); for (int i = 1; i <= ws.UsedRange.Columns.Count; i++) { string str = ((Microsoft.Office.Interop.Excel.Range)ws.UsedRange.Cells[1, i]).Text.ToString(); cmbWorkli.Items.Add(str); } cmbWorkli.SelectedIndex = 0; #endregion } catch { } finally { appExcel.Quit(); if (appExcel != null) { appExcel = null; } } } int rowCount = 0, colSave = 0, colSelect = 0; Thread tr; void btnCreatePying_Click(object sender, EventArgs e) { if (cmbWorksheet.Text.Trim() == "") { MessageBox.Show("请选择工作表"); return; } //this.Parent.Parent.Hide(); if (cmbWorksheet.Text.Trim() == "请选择") { MessageBox.Show("请选择工作表"); return; } if (cmbWorkli.Text.Trim() == "请选择字段" || cmbWorkli.Text.Trim() == "") { MessageBox.Show("请选择要操作的列"); return; } if (cmbSave.Text.Trim() == "") { MessageBox.Show("请选择保存列"); return; } try { appExcel = new Microsoft.Office.Interop.Excel.Application();//实例化EXCEL对像 WorkBook = appExcel.Workbooks.Open(txtFileName.Text, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//实例化工作薄 //获取工作表 ws = (Microsoft.Office.Interop.Excel.Worksheet)WorkBook.Worksheets[int.Parse(cmbWorksheet.SelectedIndex.ToString())]; rowCount = ws.UsedRange.Rows.Count; //设置新保存列的列头 ((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, int.Parse(cmbSave.Text.ToString())]).Value2 = "简拼"; //将选择的列转换成拼音后保存 colSave = int.Parse(cmbSave.Text); colSelect = cmbWorkli.SelectedIndex; tr = new Thread(CreatePying); tr.Start(ws); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (!tr.IsAlive) { appExcel.Quit(); if (appExcel != null) { appExcel = null; } } } } public void CreatePying(object wsssss) { ws = (Microsoft.Office.Interop.Excel.Worksheet)wsssss; for (int i = 2; i <= rowCount; i++) { ((Microsoft.Office.Interop.Excel.Range)ws.Cells[i, colSave]).Value2 = Hz2Py.Convert(((Microsoft.Office.Interop.Excel.Range)ws.Cells[i, colSelect]).Text.ToString()); } MessageBox.Show("操作成功"); WorkBook.Save(); } 我的意思是 Excel表是用的是gradview控件绑定数据源的方法呈现在网页的。。那么在后台还可以用SQL 对呈现出来的Excel表进行操作吗~~~比如 select insert update delete 之类的~~~ 可以, select * from $sheet1 请教asp.net的问题,带一些ajax的问题和wcf的问题 c# 服务端去解析dom元素,或者xml,或者其它方法实现 求一个函数 第一次做系统,大家指教一下! datagredview 中,如何绑定数据库的数据源 关于C#字体的设置 急等 为什么,我从txt文件中倒出的数据,第一条记录总是被当作字段名? 怎样得到表中的序列!并查询??? c#制作的dll,asp中使用 c# winform问题 关于继承中new的疑问 正则表达式提取特定的URI链接地址
Microsoft.Office.Interop.Excel.Application appExcel = null;//创建EXCEL对像
Microsoft.Office.Interop.Excel.Workbook WorkBook;//创建工作薄
Microsoft.Office.Interop.Excel.Worksheet ws = null;//创建工作表 private void UserControl1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 256; i++)
{
cmbSave.Items.Add(i.ToString());
}
} private void btnLL_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "(*.xls)|*.xls";//对话框只读取EXCEL文件夹
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog1.FileName;
try
{
cmbWorksheet.Items.Clear(); appExcel = new Microsoft.Office.Interop.Excel.Application();//实例化EXCEL对像
WorkBook = appExcel.Workbooks.Open(txtFileName.Text, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//实例化工作薄 #region 将工作表的名称读到cmbworksheet中
cmbWorksheet.Items.Add("请选择");
for (int i = 1; i <= WorkBook.Worksheets.Count; i++)
{
ws = (Microsoft.Office.Interop.Excel.Worksheet)WorkBook.Worksheets[i];
cmbWorksheet.Items.Add(ws.Name.ToString()); }
cmbWorksheet.SelectedIndex = 0;
#endregion
}
catch
{ }
finally
{
appExcel.Quit();
appExcel = null;
}
}
} private void cmbWorksheet_SelectedIndexChanged(object sender, EventArgs e)
{
cmbWorkli.Items.Clear();
if (cmbWorksheet.Text == "请选择")
{
return;
}
try
{
appExcel = new Microsoft.Office.Interop.Excel.Application();//实例化EXCEL对像
WorkBook = appExcel.Workbooks.Open(txtFileName.Text, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//实例化工作薄
//获取工作表
ws = (Microsoft.Office.Interop.Excel.Worksheet)WorkBook.Worksheets[int.Parse(cmbWorksheet.SelectedIndex.ToString())];
#region 读取工作表中的列头到cmbWorkli控件中
cmbWorkli.Items.Add("请选择字段");
for (int i = 1; i <= ws.UsedRange.Columns.Count; i++)
{
string str = ((Microsoft.Office.Interop.Excel.Range)ws.UsedRange.Cells[1, i]).Text.ToString();
cmbWorkli.Items.Add(str);
}
cmbWorkli.SelectedIndex = 0;
#endregion
}
catch
{ }
finally
{
appExcel.Quit();
if (appExcel != null)
{
appExcel = null;
}
}
} int rowCount = 0, colSave = 0, colSelect = 0; Thread tr;
void btnCreatePying_Click(object sender, EventArgs e)
{
if (cmbWorksheet.Text.Trim() == "")
{
MessageBox.Show("请选择工作表");
return;
}
//this.Parent.Parent.Hide();
if (cmbWorksheet.Text.Trim() == "请选择")
{
MessageBox.Show("请选择工作表");
return;
}
if (cmbWorkli.Text.Trim() == "请选择字段" || cmbWorkli.Text.Trim() == "")
{
MessageBox.Show("请选择要操作的列");
return;
}
if (cmbSave.Text.Trim() == "")
{
MessageBox.Show("请选择保存列");
return;
}
try
{ appExcel = new Microsoft.Office.Interop.Excel.Application();//实例化EXCEL对像
WorkBook = appExcel.Workbooks.Open(txtFileName.Text, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//实例化工作薄
//获取工作表 ws = (Microsoft.Office.Interop.Excel.Worksheet)WorkBook.Worksheets[int.Parse(cmbWorksheet.SelectedIndex.ToString())];
rowCount = ws.UsedRange.Rows.Count;
//设置新保存列的列头
((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, int.Parse(cmbSave.Text.ToString())]).Value2 = "简拼";
//将选择的列转换成拼音后保存
colSave = int.Parse(cmbSave.Text);
colSelect = cmbWorkli.SelectedIndex;
tr = new Thread(CreatePying);
tr.Start(ws); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (!tr.IsAlive)
{ appExcel.Quit();
if (appExcel != null)
{
appExcel = null;
}
}
}
} public void CreatePying(object wsssss)
{
ws = (Microsoft.Office.Interop.Excel.Worksheet)wsssss;
for (int i = 2; i <= rowCount; i++)
{
((Microsoft.Office.Interop.Excel.Range)ws.Cells[i, colSave]).Value2 = Hz2Py.Convert(((Microsoft.Office.Interop.Excel.Range)ws.Cells[i, colSelect]).Text.ToString());
}
MessageBox.Show("操作成功");
WorkBook.Save(); }
那么在后台还可以用SQL 对呈现出来的Excel表进行操作吗~~~
比如 select insert update delete 之类的~~~