为什么[import java.awt。*]不包含[import java.awt.event.ActionEvent]?(Why does [import java.awt.*] not also include [import java.awt.event.ActionEvent]?)
晚间,
我正在玩一个小摇摆应用程序,并且我添加了一个按钮来响应被按下。 因此我需要实现ActionListener。 我已经添加了这一行:
import java.awt.*;
但它告诉我它找不到“ActionListener”类。 所以过了一会儿,我结束了这个:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
我假设awt。*部分意味着它将在awt伞下导入所有子包。 显然情况并非如此。 有人能为我澄清这个问题吗?
Evening,
I was playing around with a little swing application, and I added a button which was to respond to being pressed. Therefore I needed to implement ActionListener. I had already added this line:
import java.awt.*;
but it informed me that it could not find the "ActionListener" class. So after a few moments I ended up with this:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
I was under the assumption that the awt.* part meant that it was importing all sub-packages under the awt umbrella. Apparently this isn't the case. Can someone please clarify this problem for me?
原文:https://stackoverflow.com/questions/11627001
最满意答案
这是您如何使用
MemoryStream
创建新的电子表格文档,然后将其保存为Byte[]
Byte[] file; using (MemoryStream mem = new MemoryStream()) { using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(mem, SpreadsheetDocumentType.Workbook)) { WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart(); workbookpart.Workbook = new Workbook(); WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>(); worksheetPart.Worksheet = new Worksheet(new SheetData()); Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook. AppendChild<Sheets>(new Sheets()); Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart. GetIdOfPart(worksheetPart), SheetId = 1, Name = "aStupidSheet" }; sheets.Append(sheet); workbookpart.Workbook.Save() spreadsheetDocument.Close(); } file = mem.ToArray(); }
With the help from FortyTwo I managed to achieve my goal.
So the desired "workflow" is:
- copy
.docx
template (added headers or other things) to memory- edit content of the document
- return this document as
FileResult
without saving the changes to templateSo here is the code:
public FileResult EditDOCXBody() { // Prepare file path string file = "../WordTemplates/EmptyTemplate.docx"; String fullFilePath = HttpContext.ApplicationInstance.Server.MapPath(file); // Copy file content to MemeoryStream via byte array MemoryStream stream = new MemoryStream(); byte[] fileBytesArray = System.IO.File.ReadAllBytes(fullFilePath); stream.Write(fileBytesArray, 0, fileBytesArray.Length); // copy file content to MemoryStream stream.Position = 0; // Edit word document content using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true)) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; Body body = mainPart.Document.Body; // add some text to body body.Append(new Paragraph( new Run( new Text("Current time is: " + DateTime.Now.ToLongTimeString())))); // Save changes to reflect on stream object mainPart.Document.Save(); } return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "servedFilename.docx"); }
Some important notes:
- you need to write file bytes to MemoryStream manually otherwise you get Memory stream is not expandable error. More info here
- you have to use
mainPart.Document.Save()
for changes to take effect onMemoryStream
- when returning the file, you have to use
.ToArray()
otherwise the returned file is corrupted
相关问答
更多-
OpenXML与Word DOCX文件中的mailmerge字段 - 完整示例(OpenXML with mailmerge fields in a Word DOCX file - full example)[2024-01-19]
这是一个例子 。 它使用docx4j.NET - 这是开源的,你可以从Nuget获得 - 而不是微软的Open XML SDK。 Here's an example. It uses docx4j.NET - which is open source and which you can get from Nuget - not Microsoft's Open XML SDK. -
通过OpenXml编辑MailMerge(Edit MailMerge via OpenXml)[2023-08-12]
如果您已经在Settings Part XML的相关部分中拥有相应的rID(关系ID),我相信您可以这样做。 我给出的方法依赖于Linq。 但请注意,Word可以在Settings.xml中存储两个数据源文件名,在settings.xml.rels中有两个对应的关系。 更重要的是,它们都具有相同的关系类型字符串。 AIUI Word实际上并不使用MailMerge设置的ODSO(“Office数据源对象”)部分中的数据文件名,但您应该检查。 获得旧关系的一种方法如下(这需要Linq,因为它代表): // G ... -
在通过OpenXML SDK插入时如何保持当前样式?(How to keep current style in when inserting via OpenXML SDK?)[2022-06-24]
我做了一些关于docx文件格式的调查。 显然,我不能专门谈论你正在使用的文件......但我想你可能对我发现的内容感兴趣。 如果您创建了docx文件的副本并给它一个.zip扩展名,则可以提取出文档的内容。 在我的情况下,也可能在你的情况下,文档的主要部分位于提取文件[Extraction Base Path]\word\document.xml 。 以下XML代码片段似乎适用于表格中的第一个单元格(用于我创建的文档):我推荐以下方法。 它使用Ancestor来避免递归并利用C#6中的短路Null条件运算符 。 internal static OpenXmlPart GetMainDocumentPart(OpenXmlElement xmlElement) { return xmlElement?.Ancestors()?.FirstOrDefault()?.MainDocumentPart as OpenXmlPart ?? xmlEl ... openxml无法通过sharepoint rest打开docx文件(openxml can't open docx file throught sharepoint rest)[2022-04-26]
问题在于与sdk一起打包的JZIP.js。 更好的方法是将模板保存为Word xml文件,然后通过ajax下载并打开它。 为我工作 the problem was with the JZIP.js that comes packaged with the sdk. A better approac is to save the template as a Word xml file and then download it through ajax and open it. worked for meOpenXML标题(OpenXML Headers)[2022-08-12]
好像有一个标题在文档中实际没有使用 Seems like there was a header which was not actually used in the document编辑OpenXML docx模板并将其作为FileResult返回(Edit OpenXML docx template and return it as FileResult)[2023-10-23]
这是您如何使用MemoryStream创建新的电子表格文档,然后将其保存为Byte[] Byte[] file; using (MemoryStream mem = new MemoryStream()) { using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(mem, SpreadsheetDocumentType.Workbook)) { Workbook ...OpenXML文档,当没有扩展时,你怎么知道哪个是哪个(OpenXML documents, how do you know which is which, when there is no extention)[2022-07-09]
我最终使用了System.IO.Packaging。 private string anotherOpenXmlAttempt(string file) { string ext = string.Empty; Package package = Package.Open(file); if (package.PartExists(new Uri("/word/document.xml", UriKind.Relative))) { ...如果您的意思是使用占位符进行内容控制,则可以使用以下代码: //Get SdtElement (can be a block, run... so I use the base class) with corresponding Tag SdtElement block = doc.MainDocumentPart.Document.Body.Descendants() .FirstOrDefault(sdt => sdt.SdtProperties.GetFirstChild< ... OpenXML SDK和MathML(OpenXML SDK and MathML)[2022-06-11]
据我所知,OpenXml SDK不支持开箱即用的演示MathML。 相反,OpenXml SDK支持Office MathML。 因此,要将演示MathML插入到word文档中,我们首先必须将演示MathML转换为Office MathML。 幸运的是,Microsoft提供了一个XSL文件(称为MML2OMML.xsl )来将表示MathML转换为Office MathML。 文件MML2OMML.xsl位于%ProgramFiles%\Microsoft Office\Office12 。 结合.Net ...相关文章
更多- (二)solr data import
- 自己封装的一个Solr Data Import Request Handler Scheduler
- Solr 4.3.0 配置Data import handler时出错
- solr实例代码 import org.apache.solr.client.solrj.SolrServer
- 使用Solr Data Import的delta-import功能
- solr 的delta-import
- merge solr index &&&&& very import create new core
- Solr集群架构概述及delta-import详细配置
- Solr4:配置Data Import,从数据库直接创建索引
- Solr集群架构概述及delta-import详细配置
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)