问题是因为excel数据没有保存。我也在处理同样的问题,并采用了适合我的另一种解决方案。我只是在excel中创建了一个宏来保存excel工作簿,只要它的单元格值发生了变化。现在我得到了带有最新保存数据的excel文件,这些数据可以通过java代码读取,并可用于其他目的。
我会后我的宏代码和Java代码,我用来检索从Excel中的数据,
宏代码
Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("A6:A12,B6:B12,L6:L12,O6:O12,P6:P12,Y6:Y12")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Do Until Not IsFileLocked(ThisWorkbook.Name)
If IsFileLocked(ThisWorkbook.Name) Then
Application.Wait Now + TimeSerial(0, 0, 0.5)
End If
Loop
ActiveWorkbook.Save
End If
Set KeyCells = Nothing
End Sub
Function IsFileLocked(filePath As String) As Boolean
On Error Resume Next
Open filePath For Binary Access Read Write Lock Read Write As #1
Close #1
If Err.Number <> 0 Then
IsFileLocked = True
' Application.StatusBar = "Waiting for file to close"
Err.Clear
Else
IsFileLocked = False
' Application.StatusBar = ""
End If
End Function
Java代码的
fileName="C:\\ODIN\\Diet\\Arbitrage.xls";
public boolean readExcel(String fileName)
{
ArrayList newList=null;
FileInputStream fis=null;
POIFSFileSystem poifs=null;
HSSFWorkbook hssfwb=null;
HSSFSheet hssfs=null;
Iterator rowIterate=null;
try
{
newList=new ArrayList();
fis=new FileInputStream(fileName);
poifs=new POIFSFileSystem(fis);
hssfwb=new HSSFWorkbook(poifs);
hssfs=hssfwb.getSheetAt(0);
rowIterate=hssfs.rowIterator();
while(rowIterate.hasNext())
{
HSSFRow row=(HSSFRow) rowIterate.next();
Iterator cellIterate=row.cellIterator();
ArrayList<HSSFCell> cellList=new ArrayList<>();
while(cellIterate.hasNext())
{
HSSFCell cell=(HSSFCell) cellIterate.next();
cellList.add(cell);
}
newList.add(cellList);
}
for(int i=0;i<newList.size();i++)
{
ArrayList<HSSFCell> cellList=(ArrayList<HSSFCell>) newList.get(i);
for(int j=0;j<cellList.size();j++)
{
HSSFCell cell=cellList.get(j);
System.out.println("Cell Values: "+cell);
}
System.out.println("----------------------");
}
}
catch(Exception e)
{
log.error(e.getMessage());
}
return true;
}
下一步你需要知道的是如何创建一个宏并保存它,以便在下一次重新启动时el应用程序您的宏将无需任何用户干预即可正常工作。
您的excel文件已经存在于上述位置。现在,我将解释每一个步骤,以在MS Excel中继续进行2007年
- 打开excel表
- 转到办公室按钮 - > Excel的选项 - >勾选“显示开发者选项卡”选项
- 开放开发选项卡并单击Visual Basic图标。
- 您的代码窗口现在将打开,从项目浏览器中创建一个新的模块。
- 将上面的代码粘贴到所有表单中。
- 现在您必须将宏安全设置更改为低。
- Goto Developer选项卡并单击宏安全性。
- 选择“启用所有宏”单选按钮并选中“信任访问VBA项目对象模型”选项。
- 现在保存并关闭excel,就是这样。
但是这会导致性能问题,因为保存过程将在每个单元更改时执行。
excel文件中的数据是连续变化的股票市场 – Prashant
您的问题是什么?你的代码与你的评论矛盾:该文件不是来自谷歌,而是来自c:/ ODING/...)。 –