C#操作Excel
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#操作Excel,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2598字,纯文字阅读大概需要4分钟。
内容图文
public void DoMerge(string[] source)
{
Microsoft.Office.Interop.Excel.Application sourceApp = new Microsoft.Office.Interop.Excel.Application();
Workbooks sourceWbks = sourceApp.Workbooks;
_Workbook source_wbk = sourceWbks.Add(source[0]);
Sheets source_shs = source_wbk.Sheets;
Worksheet source_wsh = (Worksheet)source_shs[2];
//判断有多少行内容
int lastRow = 2; //最后一列
for (; ; lastRow++)
{
string s = ((Range)source_wsh.Cells[lastRow, 1]).Text.ToString();
if (s == "")
break;
}
//合并文件
//0已经读作初始文件了
for (int index = 1; index < source.Length; index++)
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Workbooks wbks = app.Workbooks;
_Workbook _wbk = wbks.Add(source[index]);
Sheets shs = _wbk.Sheets;
Worksheet _wsh = (Worksheet)shs[2];
int rowCount = 2;
for (; ; rowCount++)
{
string s = ((Range)_wsh.Cells[rowCount, 1]).Text.ToString();
if (s == "")
break;
for (int colCount = 1; colCount <= MergeColCount; colCount++)
source_wsh.Cells[lastRow, colCount] = ((Range)_wsh.Cells[rowCount, colCount]).Text.ToString();
lastRow++;
}
app.Quit();
Form1.Kill(app);
}
//删除合并后的BCDNP列
DeleteCol(source_wsh, 2);
DeleteCol(source_wsh, 2);
DeleteCol(source_wsh, 2);
DeleteCol(source_wsh, 11);
DeleteCol(source_wsh, 12);
DeleteCol(source_wsh, 12);
// 改日期栏格式
//逐行处理
for (int billRow = 2; ; billRow++)
{
string s = ((Range)source_wsh.Cells[billRow, 2]).Text.ToString();
if (s == "")
break;
//单号改成最后5位数字,
source_wsh.Cells[billRow, 2] = s.Substring(s.Length - 5);
//重量件数改数字
s = ((Range)source_wsh.Cells[billRow, 7]).Text.ToString();
Range myrange = (Range)source_wsh.Cells[billRow, 7];
myrange.NumberFormatLocal = "0.00_ ";
source_wsh.Cells[billRow, 7] = s;
myrange = (Range)source_wsh.Cells[billRow, 8];
myrange.NumberFormatLocal = "0";
source_wsh.Cells[billRow, 8] = ((Range)source_wsh.Cells[billRow, 8]).Text.ToString();
//修改日期栏格式
myrange = (Range)source_wsh.Cells[billRow, 1];
myrange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignGeneral;
myrange.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
myrange.ShrinkToFit = true;
source_wsh.Cells[billRow, 1] = ((Range)source_wsh.Cells[billRow, 1]).Text.ToString();
}
FileInfo f = new FileInfo(source[0]);
string fileName = f.DirectoryName + "\\" + DateTime.Now.ToString("yyyyMMdd") + "转运单.xlsx";
SaveFileDialog sfd = new SaveFileDialog();
//设置文件类型
sfd.FileName = fileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
source_wbk.SaveAs(sfd.FileName);
}
sourceApp.Quit();
Form1.Kill(sourceApp);//调用kill当前excel进程
}
原文:http://www.cnblogs.com/punkrocker/p/6049015.html
内容总结
以上是互联网集市为您收集整理的C#操作Excel全部内容,希望文章能够帮你解决C#操作Excel所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。