C#操作Excel

來源:互聯網
上載者:User

標籤:alignment   .text   file   break   form   tostring   book   books   for   

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進程
}

C#操作Excel

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.