Leabharlann .NET saor in aisce chun Doiciméid Phróiseála Focal a Chruthú

Léigh, Scríobh, Ionramháil & Tiontaigh comhaid Word trí Open Source .NET API.

Is API foinse oscailte é NetOffice a ligeann d’fhorbróirí .NET Microsoft Office a uathoibriú agus Breiseáin Microsoft Office a fhorbairt. Ag baint úsáide as an API, is féidir leis an bhforbróir na gnéithe go léir atá san áireamh i leaganacha MS Office 2000, 2002, 2003, 2007, 2010, 2013, agus 2016 a úsáid. Tá an API bunaithe ar COM-ailtireacht áit a ndéanann tú réada seachfhreastalaí COM a aisghabháil i d'iarratas agus tú. caithfidh tú gach réad seachfhreastalaí COM a shaoradh. Is é an príomhbhuntáiste a bhaineann le NetOffice a úsáid ná an cumas oibriú le leagan difriúil de Microsoft Office ag aon am amháin agus cuireann sé meicníocht chosanta ar fáil chun seachvótálaithe COM a bhainistiú.

Trí úsáid a bhaint as an API, is féidir leat feidhmchlár a fhorbairt do chórais oibriúcháin 32-giotán agus 64-giotán. Is féidir leat tionóil NetOffice a úsáid i ngach cás gan imní ar bith. Soláthraíonn an API dornán leabharlann chun doiciméid oifige a ionramháil. Chun a bheith ag obair le doiciméid Microsoft Word, ní mór duit WordApi.dll le OfficeApi.ddl, VBIDEApi.dll, agus NetOffice.dll mar spleáchais.

Previous Next

Tús a chur le NetOffice

Ar an gcéad dul síos, ní mór duit .NET Framework 4.5 nó níos airde a bheith agat. Ina dhiaidh sin, íoslódáil an stór de láimh de láimh ó GitHub nó suiteáil é ó NuGet.

Suiteáil  NetOffice ó NuGet

 Install-Package NetOfficeFw.Word

Cruthaigh Doiciméad Word ag baint úsáide as API C# Saor in Aisce

Ceadaíonn NetOffice do ríomhchláraitheoirí .NET Comhaid Microsoft Word a chruthú go ríomhchláraithe. Chun comhad focal a chruthú agus, ar dtús, ní mór duit feidhmchlár Word a thúsú agus boscaí teachtaireachta a mhúchadh. Nuair a bheidh d’iarratas focal tosaithe is féidir leat doiciméad nua a chur leis trí úsáid a bhaint as modh WordApplicaiton.Documents.Add(). Is féidir leat téacs a chur isteach i do chomhad focal nuachruthaithe trí úsáid a bhaint as modh WrodApplication.Selection.TypeText() agus cló a shocrú ag baint úsáide as modh WordApplicaiton.Selection.Font(). Nuair a bheidh do dhoiciméad críochnaithe agat, is féidir leat é a shábháil ag baint úsáide as modh Document.SaveAs().

Cruthaigh Doiciméad Word trí Leabharlann NetOffice

 
  // start word and turn off msg boxes
  Word.Application wordApplication = new Word.Application();
  wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;
  // create a utils instance, no need for but helpful to keep the lines of code low
  CommonUtils utils = new CommonUtils(wordApplication);
  // add a new document
  Word.Document newDocument = wordApplication.Documents.Add();
  // insert some text
  wordApplication.Selection.TypeText("This text is written by automation");
  wordApplication.Selection.HomeKey(WdUnits.wdLine, WdMovementType.wdExtend);
  wordApplication.Selection.Font.Color = WdColor.wdColorSeaGreen;
  wordApplication.Selection.Font.Bold = 1;
  wordApplication.Selection.Font.Size = 18;
  // save the document
  string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example01", DocumentFormat.Normal);
  newDocument.SaveAs(documentFile);
  // close word and dispose reference
  wordApplication.Quit();
  wordApplication.Dispose();
  // show end dialog
  HostApplication.ShowFinishDialog(null, documentFile);

Cruthaigh Tábla i Word ag úsáid C#

Ceadaíonn NetOffice do ríomhchláraitheoirí .NET Táblaí a chur leis i Microsoft Word File de réir ríomhchláraithe. Chun táblaí a chur i gcomhad Word ar dtús, ní mór duit feidhmchlár Word a thosú agus boscaí teachtaireachta a mhúchadh agus doiciméid nua a chur leis ag baint úsáide as modh WordApplicaiton.Documents.Add(). Is féidir leat tábla a chur isteach i do chomhad focal nuachruthaithe trí Word.Table a thúsú agus raon tábla a shocrú ag baint úsáide as modh Tables.Add (wordApplication.Selection.Range, 3, 2). Is féidir leat téacs a chur isteach i do chealla trí chill ar leith a roghnú ag baint úsáide as table.Cell(1, 1). Roghnaigh() agus cuir isteach téacs ann. Nuair a bheidh do dhoiciméad críochnaithe agat, is féidir leat é a shábháil ag baint úsáide as modh Document.SaveAs().

Cruthaigh Tábla i Word File via .NET


  Word.Application wordApplication = new Word.Application();
  wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;
  // create a utils instance, not need for but helpful to keep the lines of code low
  CommonUtils utils = new CommonUtils(wordApplication);
  // add a new document
  Word.Document newDocument = wordApplication.Documents.Add();
  // add a table
  Word.Table table = newDocument.Tables.Add(wordApplication.Selection.Range, 3, 2);
  // insert some text into the cells
  table.Cell(1, 1).Select();
  wordApplication.Selection.TypeText("This");
  table.Cell(1, 2).Select();
  wordApplication.Selection.TypeText("table");
  table.Cell(2, 1).Select();
  wordApplication.Selection.TypeText("was");
  table.Cell(2, 2).Select();
  wordApplication.Selection.TypeText("created");
  table.Cell(3, 1).Select();
  wordApplication.Selection.TypeText("by");
  table.Cell(3, 2).Select();
  wordApplication.Selection.TypeText("NetOffice");
  // save the document
  string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example02", DocumentFormat.Normal);
  newDocument.SaveAs(documentFile);
  // close word and dispose reference
  wordApplication.Quit();
  wordApplication.Dispose();
  // show end dialog
  HostApplication.ShowFinishDialog(null, documentFile);
 Gaeilge