CSharp examples for Office:Excel
Process Excel file
using System.Text; using System;//from ww w . j av a2 s . co m using Microsoft.VisualBasic; public class Main{ /// <summary> /// /// </summary> /// <param name="p"></param> public static void BatchConvert(string ExcelFilename) { dynamic excelObj = Interaction.CreateObject("Excel.Application"); excelObj.Visible = true; dynamic workbook; workbook = excelObj.Workbooks.Open(ExcelFilename); dynamic worksheet = workbook.Sheets(1); int row = 5; while (!string.IsNullOrEmpty(worksheet.Cells(row,2).Text)) { if (!string.IsNullOrEmpty(worksheet.Cells(row, 3).Text)) { worksheet.Cells(row, 4).Value = native2Ascii(worksheet.Cells(row, 3).Text); } else { worksheet.Cells(row, 3).Value = ascii2Native(worksheet.Cells(row, 4).Text); } row++; } workbook.Save(); workbook.Close(); excelObj.Quit(); excelObj = null; } /// <summary> /// /// </summary> /// <param name="str"></param> /// <returns></returns> public static string ascii2Native(string str) { StringBuilder sb = new StringBuilder(); int begin = 0; int index = str.IndexOf(PREFIX); while (index != -1) { sb.Append(str.Substring(begin, index - begin)); sb.Append(ascii2Char(str.Substring(index, 6))); begin = index + 6; index = str.IndexOf(PREFIX, begin); } sb.Append(str.Substring(begin)); return sb.ToString(); } /// <summary> /// /// </summary> /// <param name="str"></param> /// <returns></returns> public static string native2Ascii(string str) { char[] chars = str.ToCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < chars.Length; i++) { sb.Append(char2Ascii(chars[i])); } return sb.ToString(); } }